Commit a5d83ec9 authored by Adrien Oliva's avatar Adrien Oliva
Browse files

Add more unittests

parent 2c084bad
# Generated by YCM Generator at 2016-11-22 18:03:59.743700
# Generated by YCM Generator at 2018-05-28 22:33:19.053373
# This file is NOT licensed under the GPLv3, which is the license for the rest
# of YouCompleteMe.
......@@ -39,9 +39,11 @@ flags = [
'-DHAVE_CONFIG_H',
'-DPIC',
'-I.',
'-Ilibavl',
'-Iutests',
'-Wall',
'-Werror',
'-Wextra',
'-std=c11',
]
......
......@@ -54,6 +54,9 @@ TEST(AvlDefaultDict, MultipleInsert)
}
for (int i = 0; i < 100; i++)
UNSIGNED_LONGS_EQUAL(i + 1, insert_elmt(first, &data[i], sizeof(int)));
/* Verify that tree is always an AVL-tree */
verif_tree(first);
}
TEST(AvlDefaultDict, InsertNoData)
......
/*
* Libavl is a library to manage AVL structure to store and organize
* everykind of data. You just need to implement function to compare,
* to desallocate and to print your structure.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2016 Adrien Oliva <adrien.oliva@yapbreak.fr>
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*/
#ifndef UT_STRUCT_H_CZM72EHS
#define UT_STRUCT_H_CZM72EHS
#include <avl.h>
#include <CppUTest/TestHarness.h>
TEST_GROUP(AvlTreeStructure)
{
tree *avl;
struct avl_data_s {
int key;
char value;
};
static int data_compare(void *a, void *b)
{
struct avl_data_s *aa = static_cast<struct avl_data_s *>(a);
struct avl_data_s *bb = static_cast<struct avl_data_s *>(b);
return aa->key - bb->key;
};
static void data_delete(void *d)
{
free(static_cast<struct avl_data_s *>(d));
}
static void data_copy(void *s, void *d, size_t length)
{
struct avl_data_s *src = static_cast<struct avl_data_s *>(s);
struct avl_data_s *dst = static_cast<struct avl_data_s *>(d);
(void) length;
dst->key = src->key;
dst->value = src->value;
}
void setup()
{
avl = init_dictionnary(data_compare, NULL, data_delete, data_copy);
};
void teardown()
{
delete_tree(avl);
};
};
TEST(AvlTreeStructure, SingleInsert)
{
struct avl_data_s data = { .key = 42, .value = 'a' };
UNSIGNED_LONGS_EQUAL(1, insert_elmt(avl, &data, sizeof(struct avl_data_s)));
struct avl_data_s to_find;
to_find.key = 42;
LONGS_EQUAL(1, is_present(avl, &to_find));
LONGS_EQUAL(1, get_data(avl, &to_find, sizeof(struct avl_data_s)));
BYTES_EQUAL(data.value, to_find.value);
}
TEST(AvlTreeStructure, MultipleInsertWithGet)
{
struct avl_data_s data[100] = { };
for (int i = 0; i < 50; i++) {
data[i].key = 2 * i;
data[i].value = (2 * i) % 256;
data[99 - i].key = 2 * i + 1;
data[99 - i].value = (2 * i + 1) % 256;
}
for (int i = 0; i < 100; i++)
UNSIGNED_LONGS_EQUAL(i + 1, insert_elmt(avl, &data[i], sizeof(struct avl_data_s)));
/* Verify that tree is always an AVL-tree */
verif_tree(avl);
struct avl_data_s to_find;
for (int i = 0; i < 100; i++) {
to_find.key = data[i].key;
LONGS_EQUAL(1, is_present(avl, &to_find));
LONGS_EQUAL(1, get_data(avl, &to_find, sizeof(struct avl_data_s)));
BYTES_EQUAL(data[i].value, to_find.value);
}
}
TEST(AvlTreeStructure, InsertNoData)
{
struct avl_data_s data = { .key = 42, .value = 'a' };
UNSIGNED_LONGS_EQUAL(0, insert_elmt(avl, &data, 0));
UNSIGNED_LONGS_EQUAL(0, insert_elmt(avl, NULL, sizeof(int)));
}
TEST(AvlTreeStructure, InsertWithOverwrite)
{
struct avl_data_s data = { .key = 42, .value = 'a' };
insert_elmt(avl, &data, sizeof(struct avl_data_s));
data.value = 'c';
UNSIGNED_LONGS_EQUAL(1, insert_elmt(avl, &data, sizeof(struct avl_data_s)));
struct avl_data_s to_find;
to_find.key = 42;
LONGS_EQUAL(1, is_present(avl, &to_find));
LONGS_EQUAL(1, get_data(avl, &to_find, sizeof(struct avl_data_s)));
BYTES_EQUAL('c', to_find.value);
}
struct mydata_s {
int key;
char value;
char checked;
};
TEST_GROUP(AvlExploration)
{
tree *avl;
struct mydata_s data[100] = { };
static int data_compare(void *a, void *b)
{
struct mydata_s *aa = static_cast<struct mydata_s *>(a);
struct mydata_s *bb = static_cast<struct mydata_s *>(b);
return aa->key - bb->key;
};
static void data_delete(void *d)
{
free(static_cast<struct mydata_s *>(d));
}
static void data_copy(void *s, void *d, size_t length)
{
struct mydata_s *src = static_cast<struct mydata_s *>(s);
struct mydata_s *dst = static_cast<struct mydata_s *>(d);
(void) length;
dst->key = src->key;
dst->value = src->value;
dst->checked = src->checked;
}
void setup()
{
avl = init_dictionnary(data_compare, NULL, data_delete, data_copy);
for (int i = 0; i < 100; i++) {
data[i].key = i;
data[i].value = i % 256;
data[i].checked = 0;
insert_elmt(avl, &data[i], sizeof(struct mydata_s));
}
};
void teardown()
{
delete_tree(avl);
};
};
void browse(void *data, void *param)
{
int *count = static_cast<int *>(param);
struct mydata_s * d = static_cast<struct mydata_s *>(data);
(*count)++;
d->checked = 1;
};
TEST(AvlExploration, FullExploration)
{
int count = 0;
explore_tree(avl, browse, &count);
for (int i = 0; i < 100; i++) {
struct mydata_s to_find;
to_find.key = i;
get_data(avl, &to_find, sizeof(struct mydata_s));
BYTES_EQUAL(1, to_find.checked);
}
LONGS_EQUAL(100, count);
};
int accu_browse(void *data, void *param)
{
int *count = static_cast<int *>(param);
struct mydata_s * d = static_cast<struct mydata_s *>(data);
(*count)++;
d->checked = 1;
return 1;
};
TEST(AvlExploration, RestrainExploration)
{
int count = 0;
struct mydata_s min;
struct mydata_s max;
min.key = 40;
max.key = 59;
LONGS_EQUAL(20, explore_restrain_tree(avl, accu_browse, &count, &min, &max));
LONGS_EQUAL(20, count);
for (int i = 0; i < 100; i++) {
struct mydata_s to_find;
to_find.key = i;
get_data(avl, &to_find, sizeof(struct mydata_s));
if (i < 40 || i > 59) {
BYTES_EQUAL(0, to_find.checked);
} else {
printf("%d, %d, %d\n", to_find.key, to_find.value, to_find.checked);
BYTES_EQUAL(1, to_find.checked);
}
}
};
#endif /* end of include guard: UT_STRUCT_H_CZM72EHS */
......@@ -2,6 +2,7 @@
#include "ut_null.h"
#include "ut_default.h"
#include "ut_struct.h"
#include <CppUTest/CommandLineTestRunner.h>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment