1 /* Copyright (c) 2018-2020, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
6 #include "lib/cc/torint.h"
7 #include "lib/container/namemap.h"
8 #include "lib/container/namemap_st.h"
9 #include "lib/malloc/malloc.h"
15 test_namemap_empty(void *arg
)
21 namemap_t m2
= NAMEMAP_INIT();
23 tt_uint_op(0, OP_EQ
, namemap_get_size(&m
));
24 tt_uint_op(NAMEMAP_ERR
, OP_EQ
, namemap_get_id(&m
, "hello"));
25 tt_uint_op(NAMEMAP_ERR
, OP_EQ
, namemap_get_id(&m
, "hello"));
26 tt_uint_op(NAMEMAP_ERR
, OP_EQ
, namemap_get_id(&m
, "hello128"));
27 tt_uint_op(NAMEMAP_ERR
, OP_EQ
, namemap_get_id(&m
, ""));
28 tt_uint_op(0, OP_EQ
, namemap_get_size(&m
));
30 tt_uint_op(0, OP_EQ
, namemap_get_size(&m2
));
31 tt_uint_op(NAMEMAP_ERR
, OP_EQ
, namemap_get_id(&m2
, "hello"));
32 tt_uint_op(NAMEMAP_ERR
, OP_EQ
, namemap_get_id(&m2
, "hello"));
33 tt_uint_op(NAMEMAP_ERR
, OP_EQ
, namemap_get_id(&m2
, "hello128"));
34 tt_uint_op(NAMEMAP_ERR
, OP_EQ
, namemap_get_id(&m2
, ""));
35 tt_uint_op(0, OP_EQ
, namemap_get_size(&m
));
43 test_namemap_toolong(void *arg
)
51 ok
= tor_malloc_zero(MAX_NAMEMAP_NAME_LEN
+1);
52 memset(ok
, 'x', MAX_NAMEMAP_NAME_LEN
);
54 toolong
= tor_malloc_zero(MAX_NAMEMAP_NAME_LEN
+2);
55 memset(toolong
, 'x', MAX_NAMEMAP_NAME_LEN
+1);
57 tt_uint_op(NAMEMAP_ERR
, OP_EQ
, namemap_get_id(&m
, ok
));
58 tt_uint_op(NAMEMAP_ERR
, OP_EQ
, namemap_get_id(&m
, toolong
));
59 unsigned u1
= namemap_get_or_create_id(&m
, toolong
);
60 unsigned u2
= namemap_get_or_create_id(&m
, ok
);
61 tt_uint_op(u1
, OP_EQ
, NAMEMAP_ERR
);
62 tt_uint_op(u2
, OP_NE
, NAMEMAP_ERR
);
63 tt_uint_op(u2
, OP_EQ
, namemap_get_id(&m
, ok
));
64 tt_uint_op(NAMEMAP_ERR
, OP_EQ
, namemap_get_id(&m
, toolong
));
66 tt_str_op(ok
, OP_EQ
, namemap_get_name(&m
, u2
));
67 tt_ptr_op(NULL
, OP_EQ
, namemap_get_name(&m
, u1
));
76 test_namemap_blackbox(void *arg
)
84 unsigned u1
= namemap_get_or_create_id(&m1
, "hello");
85 unsigned u2
= namemap_get_or_create_id(&m1
, "world");
86 tt_uint_op(u1
, OP_NE
, NAMEMAP_ERR
);
87 tt_uint_op(u2
, OP_NE
, NAMEMAP_ERR
);
88 tt_uint_op(u1
, OP_NE
, u2
);
90 tt_uint_op(u1
, OP_EQ
, namemap_get_id(&m1
, "hello"));
91 tt_uint_op(u1
, OP_EQ
, namemap_get_or_create_id(&m1
, "hello"));
92 tt_uint_op(u2
, OP_EQ
, namemap_get_id(&m1
, "world"));
93 tt_uint_op(u2
, OP_EQ
, namemap_get_or_create_id(&m1
, "world"));
95 tt_uint_op(NAMEMAP_ERR
, OP_EQ
, namemap_get_id(&m1
, "HELLO"));
96 tt_uint_op(NAMEMAP_ERR
, OP_EQ
, namemap_get_id(&m2
, "hello"));
98 unsigned u3
= namemap_get_or_create_id(&m2
, "hola");
99 tt_uint_op(u3
, OP_NE
, NAMEMAP_ERR
);
100 tt_uint_op(NAMEMAP_ERR
, OP_EQ
, namemap_get_id(&m1
, "hola"));
101 tt_uint_op(u3
, OP_EQ
, namemap_get_or_create_id(&m2
, "hola"));
102 tt_uint_op(u3
, OP_EQ
, namemap_get_id(&m2
, "hola"));
104 unsigned int u4
= namemap_get_or_create_id(&m1
, "hola");
105 tt_uint_op(u4
, OP_NE
, NAMEMAP_ERR
);
106 tt_uint_op(u4
, OP_EQ
, namemap_get_id(&m1
, "hola"));
107 tt_uint_op(u3
, OP_EQ
, namemap_get_id(&m2
, "hola"));
109 tt_str_op("hello", OP_EQ
, namemap_get_name(&m1
, u1
));
110 tt_str_op("world", OP_EQ
, namemap_get_name(&m1
, u2
));
111 tt_str_op("hola", OP_EQ
, namemap_get_name(&m2
, u3
));
112 tt_str_op("hola", OP_EQ
, namemap_get_name(&m1
, u4
));
114 tt_ptr_op(NULL
, OP_EQ
, namemap_get_name(&m2
, u3
+ 10));
122 test_namemap_internals(void *arg
)
125 // This test actually assumes know something about the identity layout.
129 tt_uint_op(0, OP_EQ
, namemap_get_or_create_id(&m
, "that"));
130 tt_uint_op(0, OP_EQ
, namemap_get_or_create_id(&m
, "that"));
131 tt_uint_op(1, OP_EQ
, namemap_get_or_create_id(&m
, "is"));
132 tt_uint_op(1, OP_EQ
, namemap_get_or_create_id(&m
, "is"));
134 tt_uint_op(0, OP_EQ
, namemap_get_id(&m
, "that"));
135 tt_uint_op(0, OP_EQ
, namemap_get_id(&m
, "that"));
136 tt_uint_op(1, OP_EQ
, namemap_get_id(&m
, "is"));
137 tt_uint_op(2, OP_EQ
, namemap_get_or_create_id(&m
, "not"));
138 tt_uint_op(1, OP_EQ
, namemap_get_or_create_id(&m
, "is"));
139 tt_uint_op(2, OP_EQ
, namemap_get_or_create_id(&m
, "not"));
146 test_namemap_fmt(void *arg
)
149 namemap_t m
= NAMEMAP_INIT();
151 unsigned a
= namemap_get_or_create_id(&m
, "greetings");
152 unsigned b
= namemap_get_or_create_id(&m
, "earthlings");
154 tt_str_op(namemap_fmt_name(&m
, a
), OP_EQ
, "greetings");
155 tt_str_op(namemap_fmt_name(&m
, b
), OP_EQ
, "earthlings");
156 tt_int_op(a
, OP_NE
, 100);
157 tt_int_op(b
, OP_NE
, 100);
158 tt_str_op(namemap_fmt_name(&m
, 100), OP_EQ
, "{100}");
165 { #name, test_namemap_ ## name , 0, NULL, NULL }
167 struct testcase_t namemap_tests
[] = {