/* ************************************************************************** */
/* */
/* :::::::: */
/* 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);
}