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
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
* str_table.c
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
#include "config.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "str_table.h"
#include "compat.h"
#define STR(x) #x
#define STRVALUE(x) STR(x)
#define TEST_PATH STRVALUE(TESTPATH)
static char *strings[1000];
static int read_strings(void)
{
ssize_t ret;
char *line;
size_t n;
FILE *fp;
int i;
fp = fopen("words.txt", "r");
if (fp == NULL) {
perror("words.txt");
return -1;
}
for (i = 0; i < 1000; ++i) {
line = NULL;
n = 0;
ret = getline(&line, &n, fp);
if (ret < 0) {
perror("reading words");
goto fail;
}
strings[i] = line;
}
fclose(fp);
return 0;
fail:
for (i = 0; i < 1000; ++i)
free(strings[i]);
fclose(fp);
return -1;
}
int main(void)
{
str_table_t table;
size_t i, j, idx;
const char *str;
assert(chdir(TEST_PATH) == 0);
if (read_strings())
return EXIT_FAILURE;
assert(str_table_init(&table, 64) == 0);
for (i = 0; i < 1000; ++i) {
assert(str_table_get_index(&table, strings[i], &idx) == 0);
assert(idx == i);
for (j = 0; j <= i; ++j) {
str = str_table_get_string(&table, j);
assert(str != NULL);
assert(str != strings[i]);
assert(strcmp(str, strings[j]) == 0);
}
for (; j < 1000; ++j) {
str = str_table_get_string(&table, j);
assert(str == NULL);
}
}
for (i = 0; i < 1000; ++i) {
assert(str_table_get_index(&table, strings[i], &idx) == 0);
assert(idx == i);
str = str_table_get_string(&table, i);
assert(str != NULL);
assert(str != strings[i]);
assert(strcmp(str, strings[i]) == 0);
}
str_table_cleanup(&table);
for (i = 0; i < 1000; ++i)
free(strings[i]);
return EXIT_SUCCESS;
}
|