1 #include "bson.h"
2 #include "tap.h"
3 #include "test.h"
4 
5 #include <string.h>
6 #include <glib.h>
7 
8 #include <stdio.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 
15 void
16 test_bson_build_full (void)
17 {
18   bson *b, *o;
19 
20   b = bson_build_full (BSON_TYPE_DOUBLE, "double", FALSE, 3.14,
21                        BSON_TYPE_STRING, "str", FALSE, "hello world", -1,
22                        BSON_TYPE_DOCUMENT, "doc", TRUE,
23                        bson_build (BSON_TYPE_STRING, "name", "sub-document", -1,
24                                    BSON_TYPE_INT32, "answer", 42,
25                                    BSON_TYPE_NONE),
26                        BSON_TYPE_ARRAY, "array", TRUE,
27                        bson_build (BSON_TYPE_INT32, "0", 32,
28                                    BSON_TYPE_INT64, "1", (gint64)-42,
29                                    BSON_TYPE_NONE),
30                        BSON_TYPE_BINARY, "binary0", FALSE, BSON_BINARY_SUBTYPE_GENERIC,
31                        "foo\0bar", 7,
32                        BSON_TYPE_OID, "_id", FALSE, "1234567890ab",
33                        BSON_TYPE_BOOLEAN, "TRUE", FALSE, FALSE,
createSMDiagnosticError(llvm::SMDiagnostic & Diag)34                        BSON_TYPE_UTC_DATETIME, "date", FALSE, 1294860709000,
35                        BSON_TYPE_TIMESTAMP, "ts", FALSE, 1294860709000,
36                        BSON_TYPE_NULL, "null", FALSE,
37                        BSON_TYPE_REGEXP, "foobar", FALSE, "s/foo.*bar/", "i",
38                        BSON_TYPE_JS_CODE, "alert", FALSE, "alert (\"hello world!\");", -1,
39                        BSON_TYPE_SYMBOL, "sex", FALSE, "Marilyn Monroe", -1,
40                        BSON_TYPE_JS_CODE_W_SCOPE, "print", TRUE, "alert (v);", -1,
41                        bson_build (BSON_TYPE_STRING, "v", "hello world", -1,
42                                    BSON_TYPE_NONE),
43                        BSON_TYPE_INT32, "int32", FALSE, 32,
44                        BSON_TYPE_INT64, "int64", FALSE, (gint64)-42,
45                        BSON_TYPE_NONE);
46   bson_finish (b);
47 
48   o = test_bson_generate_full ();
49 
50   cmp_ok (bson_size (b), "==", bson_size (o),
51           "bson_build_full() and hand crafted BSON object sizes match");
52 
53   ok (memcmp (bson_data (b), bson_data (o), bson_size (b)) == 0,
54       "bson_build_full() and hand crafted BSON objects match");
55 
parseExampleModuleFromFile(llvm::StringRef FileName)56   bson_free (b);
57   bson_free (o);
58 
59   b = bson_build_full (BSON_TYPE_UNDEFINED, "undef", FALSE,
60                        BSON_TYPE_NONE);
61   ok (b == NULL,
62       "bson_build_full() should fail with an unsupported element type");
63   b = bson_build_full (BSON_TYPE_STRING, "str", FALSE, "hello", -1,
64                        BSON_TYPE_UNDEFINED, "undef", FALSE,
65                        BSON_TYPE_NONE);
66   ok (b == NULL,
67       "bson_build_full() should fail with an unsupported element type");
68 
69 }
70 
71 RUN_TEST (4, bson_build_full);
72