1 #include <test.h>
2 
3 #include <class.h>
4 
test_class_ref(void)5 static void test_class_ref(void)
6 {
7     {
8         ClassRef ref = ClassRefParse("class");
9         assert_true(ref.ns == NULL);
10         assert_string_equal("class", ref.name);
11         char *expr = ClassRefToString(ref.ns, ref.name);
12         assert_string_equal("class", expr);
13         free(expr);
14         ClassRefDestroy(ref);
15     }
16 
17     {
18         ClassRef ref = ClassRefParse("default:class");
19         assert_string_equal("default", ref.ns);
20         assert_string_equal("class", ref.name);
21         char *expr = ClassRefToString(ref.ns, ref.name);
22         assert_string_equal("class", expr);
23         free(expr);
24         ClassRefDestroy(ref);
25     }
26 
27     {
28         ClassRef ref = ClassRefParse("ns:class");
29         assert_string_equal("ns", ref.ns);
30         assert_string_equal("class", ref.name);
31         char *expr = ClassRefToString(ref.ns, ref.name);
32         assert_string_equal("ns:class", expr);
33         free(expr);
34         ClassRefDestroy(ref);
35     }
36 }
37 
test_ns(void)38 static void test_ns(void)
39 {
40     {
41         ClassTable *t = ClassTableNew();
42         assert_false(ClassTablePut(t, "foo", "127.0.0.1", true, CONTEXT_SCOPE_BUNDLE, NULL, NULL));
43         Class *cls = ClassTableGet(t, "foo", "127_0_0_1");
44         assert_string_equal("foo", cls->ns);
45         assert_string_equal("127_0_0_1", cls->name);
46         assert_true(cls->is_soft);
47 
48         cls = ClassTableMatch(t, "foo:127_0_.*");
49         assert_true(cls);
50         cls = ClassTableMatch(t, "foo:127_1_.*");
51         assert_false(cls);
52         cls = ClassTableMatch(t, "127_0_.*");
53         assert_false(cls);
54 
55         ClassTableDestroy(t);
56     }
57 }
58 
test_default_ns(void)59 static void test_default_ns(void)
60 {
61     {
62         ClassTable *t = ClassTableNew();
63         assert_false(ClassTablePut(t, NULL, "127.0.0.1", false, CONTEXT_SCOPE_NAMESPACE, NULL, NULL));
64         Class *cls = ClassTableGet(t, NULL, "127_0_0_1");
65         assert_true(cls != NULL);
66         assert_true(cls->ns == NULL);
67         cls = ClassTableGet(t, "default", "127_0_0_1");
68         assert_true(cls->ns == NULL);
69         assert_string_equal("127_0_0_1", cls->name);
70         assert_false(cls->is_soft);
71 
72         cls = ClassTableMatch(t, "127_0_.*");
73         assert_true(cls);
74         cls = ClassTableMatch(t, "127_1_.*");
75         assert_false(cls);
76 
77         ClassTableDestroy(t);
78     }
79 
80     {
81         ClassTable *t = ClassTableNew();
82         assert_false(ClassTablePut(t, "default", "127.0.0.1", false, CONTEXT_SCOPE_NAMESPACE, NULL, NULL));
83         Class *cls = ClassTableGet(t, NULL, "127_0_0_1");
84         assert_true(cls->ns == NULL);
85         cls = ClassTableGet(t, "default", "127_0_0_1");
86         assert_true(cls->ns == NULL);
87         assert_string_equal("127_0_0_1", cls->name);
88         assert_false(cls->is_soft);
89         ClassTableDestroy(t);
90     }
91 }
92 
test_put_replace(void)93 static void test_put_replace(void)
94 {
95     ClassTable *t = ClassTableNew();
96     assert_false(ClassTablePut(t, NULL, "test", false, CONTEXT_SCOPE_NAMESPACE, NULL, NULL));
97     Class *cls = ClassTableGet(t, NULL, "test");
98     assert_true(cls);
99     assert_int_equal(CONTEXT_SCOPE_NAMESPACE, cls->scope);
100 
101     assert_true(ClassTablePut(t, NULL, "test", true, CONTEXT_SCOPE_BUNDLE, NULL, NULL));
102     cls = ClassTableGet(t, NULL, "test");
103     assert_true(cls);
104     assert_int_equal(CONTEXT_SCOPE_BUNDLE, cls->scope);
105 
106     ClassTableDestroy(t);
107 }
108 
main()109 int main()
110 {
111     PRINT_TEST_BANNER();
112     const UnitTest tests[] =
113     {
114         unit_test(test_default_ns),
115         unit_test(test_ns),
116         unit_test(test_class_ref),
117         unit_test(test_put_replace),
118     };
119 
120     return run_tests(tests);
121 }
122