~nloomans/ft_select

ref: a4ef762331941c5e33670f7bf81eb964fb761edb ft_select/src/list2_test.c -rw-r--r-- 4.1 KiB
a4ef7623Noah Loomans fix build on linux 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   ft_select                                          :+:    :+:            */
/*                                                     +:+                    */
/*   Author: Noah Loomans                             +#+                     */
/*           <nloomans@student.codam.nl>             +#+                      */
/*                                                  #+#    #+#                */
/*   License: GPLv3                                ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include <criterion/criterion.h>
#include "list2.h"

struct	s_example
{
	int					data;
	struct s_list2_conn	conn;
};

static struct s_example	*unpack_example(struct s_list2_conn *conn)
{
	if (conn == NULL)
		return (NULL);
	return (struct s_example *)(
		(char *)conn - offsetof(struct s_example, conn));
}

Test(list2_insert, first_and_only)
{
	struct s_example	new = (struct s_example){
		.data = 42,
	};
	t_list2_meta		meta = (t_list2_meta){
		.first = NULL,
		.last = NULL,
		.len = 0,
	};

	list2_insert(&meta, NULL, &new.conn);
	cr_expect_eq(unpack_example(meta.first)->data, 42);
	cr_expect_eq(meta.first, meta.last);
	cr_expect_eq(meta.first->prev, NULL);
	cr_expect_eq(meta.first->next, NULL);
	cr_expect_eq(meta.len, 1);
}

Test(list2_insert, first_with_existing)
{
	struct s_example	new = (struct s_example){
		.data = 42,
	};
	struct s_example	existing = (struct s_example){
		.data = 21,
		.conn = {NULL, NULL},
	};
	t_list2_meta		meta = (t_list2_meta){
		.first = &existing.conn,
		.last = &existing.conn,
		.len = 1,
	};

	list2_insert(&meta, NULL, &new.conn);
	cr_expect_eq(unpack_example(meta.first), &new);
	cr_expect_eq(unpack_example(meta.last), &existing);
	cr_expect_eq(meta.first->prev, NULL);
	cr_expect_eq(meta.first->next, meta.last);
	cr_expect_eq(meta.last->prev, meta.first);
	cr_expect_eq(meta.last->next, NULL);
	cr_expect_eq(meta.len, 2);
}

Test(list2_insert, last)
{
	struct s_example	new = (struct s_example){
		.data = 42,
		.conn = {NULL, NULL},
	};
	struct s_example	existing_first = (struct s_example){
		.data = 21,
		.conn = {NULL, NULL},
	};
	struct s_example	existing_middle = (struct s_example){
		.data = 22,
		.conn = {NULL, NULL},
	};
	existing_first.conn.next = &existing_middle.conn;
	existing_middle.conn.prev = &existing_first.conn;
	t_list2_meta		meta = (t_list2_meta){
		.first = &existing_first.conn,
		.last = &existing_middle.conn,
		.len = 1,
	};

	list2_insert(&meta, &existing_middle.conn, &new.conn);
	cr_expect_eq(unpack_example(meta.first), &existing_first);
	cr_expect_eq(unpack_example(meta.first->next), &existing_middle);
	cr_expect_eq(unpack_example(meta.first->next->next), &new);
	cr_expect_eq(meta.first->next->prev, meta.first);
	cr_expect_eq(meta.first->next->next->prev, meta.first->next);
	cr_expect_eq(meta.last->prev->next, meta.last);
	cr_expect_eq(meta.len, 2);
}

Test(list2_insert, between)
{
	struct s_example	new = (struct s_example){
		.data = 42,
		.conn = {NULL, NULL},
	};
	struct s_example	existing_first = (struct s_example){
		.data = 42,
		.conn = {NULL, NULL},
	};
	struct s_example	existing_last = (struct s_example){
		.data = 42,
		.conn = {NULL, NULL},
	};
	existing_first.conn.next = &existing_last.conn;
	existing_last.conn.prev = &existing_first.conn;
	t_list2_meta		meta = (t_list2_meta){
		.first = &existing_first.conn,
		.last = &existing_last.conn,
		.len = 1,
	};

	list2_insert(&meta, &existing_first.conn, &new.conn);
	cr_expect_eq(unpack_example(meta.first), &existing_first);
	cr_expect_eq(unpack_example(meta.first->next), &new);
	cr_expect_eq(unpack_example(meta.first->next->next), &existing_last);
	cr_expect_eq(meta.first->next->prev, meta.first);
	cr_expect_eq(meta.first->next->next->prev, meta.first->next);
	cr_expect_eq(meta.last->prev->next, meta.last);
	cr_expect_eq(meta.len, 2);
}