1 #include "lib.h"
2 #include "set.h"
3
4 const char *data[] = {
5 "alpha",
6 "beta",
7 "gamma",
8 "delta",
9 "epsilon",
10 "zeta",
11 "eta",
12 "theta",
13 "iota",
14 "kappa",
15 "lambda",
16 "mu",
17 "nu",
18 "xi",
19 "omicron",
20 "pi",
21 "rho",
22 "sigma",
23 "tau",
24 "upsilon",
25 "phi",
26 "chi",
27 "psi",
28 "omega",
29 NULL,
30 };
31
32 const int n = 100000;
33
Main()34 void Main() {
35 char buf[10];
36 StrSet *set = new StrSet(Cmp, Hash);
37 for (int i = 0; i < n; i++) {
38 sprintf(buf, "%d", i);
39 set->add(new Str(buf));
40 }
41 Check(set->count() == n, "set initialization");
42 int miss = 0;
43 for (int i = 0; i < n; i++) {
44 sprintf(buf, "%d", i);
45 if (!set->contains(new Str(buf))) {
46 miss++;
47 }
48 }
49 Check(miss == 0, "contains()");
50 Check(set->count() == n, "count()");
51 Check(set->items()->len() == n, "items()");
52 set->remove(new Str("1234"));
53 Check(set->count() == n - 1, "single remove()");
54 for (int i = 0; i < n; i++) {
55 sprintf(buf, "%d", i);
56 set->remove(new Str(buf));
57 }
58 Check(set->count() == 0, "remove()");
59 StrArr *arr1 = new StrArr(NUMOF(data) - 1, data, CStrToStr);
60 StrArr *arr2 = new StrArr(data, (CStr) NULL, CStrToStr);
61 Check(arr1->eq(arr2), "array/string equality");
62 arr1 = arr1->subarr(10, arr1->len() - 10);
63 arr2->remove(0, 10);
64 Check(arr1->eq(arr2), "array ranges");
65 Str *str = S("alphalpha");
66 Check(str->substr(0, 5)->eq(str->substr(4, 5)), "substrings");
67 Check(str->find('a', 1) == 4 && str->find("alp", 1) == 4
68 && str->find("alpha", 1) == 4 && str->find("alphx") < 0,
69 "string search");
70 Check(str->rfind('l') == 5 && str->rfind("alp") == 4
71 && str->find("alx") == NOWHERE,
72 "reverse string search");
73 str = S("\x80 \x81 \xff abc");
74 Check(str->find(" \x81 ") == 1 && str->find("\x81 ") == 2
75 && str->find("\xff abc") == 4 && str->find("\xff abd") < 0,
76 "string search (signed char)");
77 Check(S("here%sthere%sbe%sdragons")->replace_all(S("%s"), S(" "))
78 ->eq("here there be dragons") &&
79 S("axbxc")->replace_count(1, S("x"), S("y"))->eq("aybxc"),
80 "string replacement");
81 }
82