static void test_bson_writer_null_realloc_2 (void) { const uint8_t testdata[] = { 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0 }; bson_writer_t *writer; uint8_t *buf = bson_malloc0(32); size_t buflen = 32; bson_t *b; int r; int i; writer = bson_writer_new(&buf, &buflen, 0, NULL, NULL); for (i=0; i<5; i++) { assert(bson_writer_begin(writer, &b)); bson_writer_end(writer); } assert(bson_writer_begin(writer, &b)); assert(!bson_append_int32(b, "a", -1, 123)); bson_writer_end(writer); r = memcmp(buf, testdata, 32); assert(r == 0); bson_writer_destroy(writer); bson_free (buf); }
static void test_bson_writer_shared_buffer (void) { bson_writer_t *writer; bson_uint8_t *buf = bson_malloc0(32); bson_bool_t rolled_back = FALSE; size_t buflen = 32; size_t n_bytes; bson_t *b; const char *key; char keystr[32]; int i = 0; int j = 0; int n_docs = 10000; writer = bson_writer_new(&buf, &buflen, 0, bson_realloc); for (i = 0; i < n_docs; i++) { bson_writer_begin(writer, &b); for (j = 0; j < 1000; j++) { bson_uint32_to_string(j, &key, keystr, sizeof keystr); assert(bson_append_int64(b, key, -1, j)); } if (bson_writer_get_length(writer) > (48 * 1024 * 1024)) { rolled_back = TRUE; bson_writer_rollback(writer); break; } else { bson_writer_end(writer); } } n_bytes = bson_writer_get_length(writer); bson_writer_destroy(writer); assert_cmpint(n_bytes, <, (48 * 1024 * 1024)); assert_cmpint(rolled_back, ==, TRUE); assert(rolled_back); bson_free(buf); }
static void test_bson_writer_empty_sequence (void) { const bson_uint8_t testdata[] = { 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0 }; bson_writer_t *writer; bson_uint8_t *buf = NULL; bson_t *b; size_t len = 0; int r; int i; writer = bson_writer_new(&buf, &len, 0, bson_realloc); for (i = 0; i < 5; i++) { bson_writer_begin(writer, &b); bson_writer_end(writer); } r = memcmp(buf, testdata, 25); assert(r == 0); bson_writer_destroy(writer); }
static void test_bson_writer_custom_realloc (void) { bson_writer_t *writer; uint8_t *buf = bson_malloc(0); size_t buflen = 0; bson_t *b; int x = 0; writer = bson_writer_new(&buf, &buflen, 0, test_bson_writer_custom_realloc_helper, &x); assert(bson_writer_begin(writer, &b)); assert(bson_append_utf8(b, "hello", -1, "world", -1)); bson_writer_end(writer); bson_writer_destroy(writer); assert_cmpint(x, >, 0); bson_free(buf); }