summaryrefslogtreecommitdiff
path: root/app/bin/unittest/catalogtest.c
blob: 70eeb22e47ca08aca6683f23297912b4515e6962 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/** \file catalog.c
* Unit tests for part catalog management
*/

#include <malloc.h>
#include <stdarg.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <setjmp.h>
#include <cmocka.h>

//#include "../common.h"
#include "../include/partcatalog.h"

ParameterLib *trackLib;
Catalog *catalog;
SearchResult* result;


// some dummy functions to suppress linking problems
const char *
wGetUserHomeDir()
{
	return("");
}

void wPrefSetString(const char* a, const char* b, const char* c)
{

}

char* wPrefGetString(const char* section, const char* name)
{
	return("");
}

void AbortProg(const char* a, const char* b, int c, const char*d)
{

}

void *
MyMalloc(size_t size)
{
	return(malloc(size));
}

void
MyFree(void* memory)
{
	free(memory);
}

char *
MyStrdup(const char* string)
{
	return(strdup(string));
}

void *
MyRealloc(void *memory, size_t size)
{
	return(realloc(memory, size));
}

static void
CreateLib(void **state)
{
	(void)state;
	trackLib = CreateLibrary(".");
	assert_non_null((void *)trackLib);
}

static void ScanEmptyDir(void **state)
{
	(void)state;
	bool success;
	DestroyCatalog(trackLib->catalog);
	success = CreateCatalogFromDir(trackLib, "//");
	assert_false(success);
}

static void ScanTestFiles(void **state)
{
	(void)state;
	bool success;
	DestroyCatalog(trackLib->catalog);
	success = CreateCatalogFromDir(trackLib, ".");
	assert_true(success);
}

static void CreateIndex(void **state)
{
	(void)state;
	unsigned int words = CreateLibraryIndex(trackLib);
	assert_true(words > 0);
}

static void SearchNothing(void **state)
{
	(void)state;
	unsigned int files = SearchLibrary(trackLib, "djfhdkljhf", result);
	assert_true(files == 0);
	SearchDiscardResult(result);
}

static void SearchSingle(void **state)
{
	(void)state;
	int files = SearchLibrary(trackLib, "peco", result );
	assert_true(files==1);
	SearchDiscardResult(result);
}

static void SearchMultiple(void **state)
{
	(void)state;
	int files = SearchLibrary(trackLib, "atlas", result);
	assert_true(files == 2);
	SearchDiscardResult(result);
}

static void FilterTest(void **state)
{
	(void)state;
	bool res;

	res = FilterKeyword("test");
	assert_false(res);
	assert_true(FilterKeyword("&"));
	assert_false(FilterKeyword("n"));
}

int main(void)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(CreateLib),
		cmocka_unit_test(ScanEmptyDir),
		cmocka_unit_test(ScanTestFiles),
		cmocka_unit_test(CreateIndex),
		cmocka_unit_test(SearchNothing),
		cmocka_unit_test(SearchSingle),
		cmocka_unit_test(SearchMultiple),
		cmocka_unit_test(FilterTest),
	};

	catalog = InitCatalog();

	return cmocka_run_group_tests(tests, NULL, NULL);
}