1 #include "tap.h"
2 #include "test.h"
3 #include "bson.h"
4 
5 #include <string.h>
6 
7 void
test_bson_regex(void)8 test_bson_regex (void)
9 {
10   bson *b;
11 
12   b = bson_new ();
13   ok (bson_append_regex (b, "regex", "foo.*bar", "i"),
14       "bson_append_regex() works");
15   bson_finish (b);
16 
17   cmp_ok (bson_size (b), "==", 23, "BSON regex element size check");
18   ok (memcmp (bson_data (b),
19               "\027\000\000\000\013\162\145\147\145\170\000\146\157\157\056"
20               "\052\142\141\162\000\151\000\000",
21               bson_size (b)) == 0,
22       "BSON regex element contents check");
23 
24   bson_free (b);
25 
26   b = bson_new ();
27   ok (bson_append_regex (b, "regex", "foo.*bar", NULL) == FALSE,
28       "bson_append_regex() without options should fail");
29   ok (bson_append_regex (b, "regex", NULL, "i") == FALSE,
30       "bson_append_regex() without a regex should fail");
31   ok (bson_append_regex (b, NULL, "foo.*bar", "i") == FALSE,
32       "bson_append_regex() should fail without a key name");
33   ok (bson_append_regex (NULL, "regex", "foo.*bar", "i") == FALSE,
34       "bson_append_regex() should fail without a BSON object");
35   bson_finish (b);
36   cmp_ok (bson_size (b), "==", 5,
37           "BSON object should be empty");
38 
39   ok (bson_append_regex (b, "regex", "foo.*bar", "i") == FALSE,
40       "Appending to a finished element should fail");
41 
42   bson_free (b);
43 }
44 
45 RUN_TEST (9, bson_regex);
46