RPM Community Forums

Mailing List Message of <rpm-cvs>

[CVS] RPM: rpm/tests/mongo/ all_types.c auth.c benchmark.c count_delet...

From: Jeff Johnson <jbj@rpm5.org>
Date: Fri 17 Sep 2010 - 22:34:09 CEST
Message-Id: <20100917203409.BE654D4FFB@rpm5.org>
  RPM Package Manager, CVS Repository
  /cvs/
  ____________________________________________________________________________

  Server: rpm5.org                         Name:   Jeff Johnson
  Root:   /v/rpm/cvs                       Email:  jbj@rpm5.org
  Module: rpm                              Date:   17-Sep-2010 22:34:09
  Branch: HEAD                             Handle: 2010091720340801

  Added files:
    rpm/tests/mongo         all_types.c auth.c benchmark.c count_delete.c
                            endian_swap.c errors.c json.c pair.c resize.c
                            simple.c sizes.c update.c

  Log:
    - mongo: stub-in the mongo-c-driver test cases.

  Summary:
    Revision    Changes     Path
    1.1         +191 -0     rpm/tests/mongo/all_types.c
    1.1         +31 -0      rpm/tests/mongo/auth.c
    1.1         +434 -0     rpm/tests/mongo/benchmark.c
    1.1         +68 -0      rpm/tests/mongo/count_delete.c
    1.1         +31 -0      rpm/tests/mongo/endian_swap.c
    1.1         +74 -0      rpm/tests/mongo/errors.c
    1.1         +169 -0     rpm/tests/mongo/json.c
    1.1         +41 -0      rpm/tests/mongo/pair.c
    1.1         +35 -0      rpm/tests/mongo/resize.c
    1.1         +103 -0     rpm/tests/mongo/simple.c
    1.1         +22 -0      rpm/tests/mongo/sizes.c
    1.1         +110 -0     rpm/tests/mongo/update.c
  ____________________________________________________________________________

  patch -p0 <<'@@ .'
  Index: rpm/tests/mongo/all_types.c
  ============================================================================
  $ cvs diff -u -r0 -r1.1 all_types.c
  --- /dev/null	2010-09-17 22:33:42.000000000 +0200
  +++ all_types.c	2010-09-17 22:34:09.284654029 +0200
  @@ -0,0 +1,191 @@
  +#include "test.h"
  +#include "bson.h"
  +#include <stdio.h>
  +#include <string.h>
  +#include <stdlib.h>
  +
  +int main(){
  +    bson_buffer bb;
  +    bson b;
  +    bson_iterator it, it2, it3;
  +    bson_oid_t oid;
  +
  +    bson_buffer_init(&bb);
  +    bson_append_double(&bb, "d", 3.14);
  +    bson_append_string(&bb, "s", "hello");
  +
  +    {
  +        bson_buffer *obj = bson_append_start_object(&bb, "o");
  +        bson_buffer *arr = bson_append_start_array(obj, "a");
  +        bson_append_binary(arr, "0", 8, "w\0rld", 5);
  +        bson_append_finish_object(arr);
  +        bson_append_finish_object(obj);
  +    }
  +
  +    bson_append_undefined(&bb, "u");
  +
  +    bson_oid_from_string(&oid, "010203040506070809101112");
  +    ASSERT(!memcmp(oid.bytes, "\x001\x002\x003\x004\x005\x006\x007\x008\x009\x010\x011\x012", 12));
  +    bson_append_oid(&bb, "oid", &oid);
  +
  +    bson_append_bool(&bb, "b", 1);
  +    bson_append_date(&bb, "date", 0x0102030405060708);
  +    bson_append_null(&bb, "n");
  +    bson_append_regex(&bb, "r", "^asdf", "imx");
  +    /* no dbref test (deprecated) */
  +    bson_append_code(&bb, "c", "function(){}");
  +    bson_append_symbol(&bb, "symbol", "SYMBOL");
  +
  +    {
  +        char hex[30];
  +        bson_buffer scope_buf;
  +        bson scope;
  +        bson_buffer_init(&scope_buf);
  +        bson_append_int(&scope_buf, "i", 123);
  +        bson_from_buffer(&scope, &scope_buf);
  +
  +        bson_append_code_w_scope(&bb, "cws", "function(){return i}", &scope);
  +        bson_destroy(&scope);
  +    }
  +
  +    /* no timestamp test (internal) */
  +    bson_append_long(&bb, "l", 0x1122334455667788);
  +
  +    bson_from_buffer(&b, &bb);
  +
  +    bson_iterator_init(&it, b.data);
  +    
  +    ASSERT(bson_iterator_more(&it));
  +    ASSERT(bson_iterator_next(&it) == bson_double);
  +    ASSERT(bson_iterator_type(&it) == bson_double);
  +    ASSERT(!strcmp(bson_iterator_key(&it), "d"));
  +    ASSERT(bson_iterator_double(&it) == 3.14);
  +
  +    ASSERT(bson_iterator_more(&it));
  +    ASSERT(bson_iterator_next(&it) == bson_string);
  +    ASSERT(bson_iterator_type(&it) == bson_string);
  +    ASSERT(!strcmp(bson_iterator_key(&it), "s"));
  +    ASSERT(!strcmp(bson_iterator_string(&it), "hello"));
  +
  +    ASSERT(bson_iterator_more(&it));
  +    ASSERT(bson_iterator_next(&it) == bson_object);
  +    ASSERT(bson_iterator_type(&it) == bson_object);
  +    ASSERT(!strcmp(bson_iterator_key(&it), "o"));
  +    bson_iterator_subiterator(&it, &it2);
  +
  +    ASSERT(bson_iterator_more(&it2));
  +    ASSERT(bson_iterator_next(&it2) == bson_array);
  +    ASSERT(bson_iterator_type(&it2) == bson_array);
  +    ASSERT(!strcmp(bson_iterator_key(&it2), "a"));
  +    bson_iterator_subiterator(&it2, &it3);
  +
  +    ASSERT(bson_iterator_more(&it3));
  +    ASSERT(bson_iterator_next(&it3) == bson_bindata);
  +    ASSERT(bson_iterator_type(&it3) == bson_bindata);
  +    ASSERT(!strcmp(bson_iterator_key(&it3), "0"));
  +    ASSERT(bson_iterator_bin_type(&it3) == 8);
  +    ASSERT(bson_iterator_bin_len(&it3) == 5);
  +    ASSERT(!memcmp(bson_iterator_bin_data(&it3), "w\0rld", 5));
  +    
  +    ASSERT(bson_iterator_more(&it3));
  +    ASSERT(bson_iterator_next(&it3) == bson_eoo);
  +    ASSERT(bson_iterator_type(&it3) == bson_eoo);
  +    ASSERT(!bson_iterator_more(&it3));
  +
  +    ASSERT(bson_iterator_more(&it2));
  +    ASSERT(bson_iterator_next(&it2) == bson_eoo);
  +    ASSERT(bson_iterator_type(&it2) == bson_eoo);
  +    ASSERT(!bson_iterator_more(&it2));
  +
  +    ASSERT(bson_iterator_more(&it));
  +    ASSERT(bson_iterator_next(&it) == bson_undefined);
  +    ASSERT(bson_iterator_type(&it) == bson_undefined);
  +    ASSERT(!strcmp(bson_iterator_key(&it), "u"));
  +
  +
  +    ASSERT(bson_iterator_more(&it));
  +    ASSERT(bson_iterator_next(&it) == bson_oid);
  +    ASSERT(bson_iterator_type(&it) == bson_oid);
  +    ASSERT(!strcmp(bson_iterator_key(&it), "oid"));
  +    ASSERT(!memcmp(bson_iterator_oid(&it)->bytes, "\x001\x002\x003\x004\x005\x006\x007\x008\x009\x010\x011\x012", 12));
  +    ASSERT(bson_iterator_oid(&it)->ints[0] == oid.ints[0]);
  +    ASSERT(bson_iterator_oid(&it)->ints[1] == oid.ints[1]);
  +    ASSERT(bson_iterator_oid(&it)->ints[2] == oid.ints[2]);
  +
  +    ASSERT(bson_iterator_more(&it));
  +    ASSERT(bson_iterator_next(&it) == bson_bool);
  +    ASSERT(bson_iterator_type(&it) == bson_bool);
  +    ASSERT(!strcmp(bson_iterator_key(&it), "b"));
  +    ASSERT(bson_iterator_bool(&it) == 1);
  +
  +    ASSERT(bson_iterator_more(&it));
  +    ASSERT(bson_iterator_next(&it) == bson_date);
  +    ASSERT(bson_iterator_type(&it) == bson_date);
  +    ASSERT(!strcmp(bson_iterator_key(&it), "date"));
  +    ASSERT(bson_iterator_date(&it) == 0x0102030405060708);
  +
  +    ASSERT(bson_iterator_more(&it));
  +    ASSERT(bson_iterator_next(&it) == bson_null);
  +    ASSERT(bson_iterator_type(&it) == bson_null);
  +    ASSERT(!strcmp(bson_iterator_key(&it), "n"));
  +
  +    ASSERT(bson_iterator_more(&it));
  +    ASSERT(bson_iterator_next(&it) == bson_regex);
  +    ASSERT(bson_iterator_type(&it) == bson_regex);
  +    ASSERT(!strcmp(bson_iterator_key(&it), "r"));
  +    ASSERT(!strcmp(bson_iterator_regex(&it), "^asdf"));
  +    ASSERT(!strcmp(bson_iterator_regex_opts(&it), "imx"));
  +
  +    ASSERT(bson_iterator_more(&it));
  +    ASSERT(bson_iterator_next(&it) == bson_code);
  +    ASSERT(bson_iterator_type(&it) == bson_code);
  +    ASSERT(!strcmp(bson_iterator_key(&it), "c"));
  +    ASSERT(!strcmp(bson_iterator_string(&it), "function(){}"));
  +    ASSERT(!strcmp(bson_iterator_code(&it), "function(){}"));
  +
  +    ASSERT(bson_iterator_more(&it));
  +    ASSERT(bson_iterator_next(&it) == bson_symbol);
  +    ASSERT(bson_iterator_type(&it) == bson_symbol);
  +    ASSERT(!strcmp(bson_iterator_key(&it), "symbol"));
  +    ASSERT(!strcmp(bson_iterator_string(&it), "SYMBOL"));
  +
  +    ASSERT(bson_iterator_more(&it));
  +    ASSERT(bson_iterator_next(&it) == bson_codewscope);
  +    ASSERT(bson_iterator_type(&it) == bson_codewscope);
  +    ASSERT(!strcmp(bson_iterator_key(&it), "cws"));
  +    ASSERT(!strcmp(bson_iterator_code(&it), "function(){return i}"));
  +
  +    {
  +        bson scope;
  +        bson_iterator_code_scope(&it, &scope);
  +        bson_iterator_init(&it2, scope.data);
  +
  +        ASSERT(bson_iterator_more(&it2));
  +        ASSERT(bson_iterator_next(&it2) == bson_int);
  +        ASSERT(bson_iterator_type(&it2) == bson_int);
  +        ASSERT(!strcmp(bson_iterator_key(&it2), "i"));
  +        ASSERT(bson_iterator_int(&it2) == 123);
  +
  +        ASSERT(bson_iterator_more(&it2));
  +        ASSERT(bson_iterator_next(&it2) == bson_eoo);
  +        ASSERT(bson_iterator_type(&it2) == bson_eoo);
  +        ASSERT(!bson_iterator_more(&it2));
  +    }
  +
  +    ASSERT(bson_iterator_more(&it));
  +    ASSERT(bson_iterator_next(&it) == bson_long);
  +    ASSERT(bson_iterator_type(&it) == bson_long);
  +    ASSERT(!strcmp(bson_iterator_key(&it), "l"));
  +    ASSERT(bson_iterator_long(&it) == 0x1122334455667788);
  +
  +    ASSERT(bson_iterator_more(&it));
  +    ASSERT(bson_iterator_next(&it) == bson_eoo);
  +    ASSERT(bson_iterator_type(&it) == bson_eoo);
  +    ASSERT(!bson_iterator_more(&it));
  +    
  +    return 0;
  +}
  +
  +
  +        
  +        
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/tests/mongo/auth.c
  ============================================================================
  $ cvs diff -u -r0 -r1.1 auth.c
  --- /dev/null	2010-09-17 22:33:42.000000000 +0200
  +++ auth.c	2010-09-17 22:34:09.314656191 +0200
  @@ -0,0 +1,31 @@
  +#include "test.h"
  +#include "mongo.h"
  +#include <stdio.h>
  +#include <string.h>
  +#include <stdlib.h>
  +
  +static mongo_connection conn[1];
  +static mongo_connection_options opts;
  +static const char* db = "test";
  +
  +int main(){
  +
  +    INIT_SOCKETS_FOR_WINDOWS;
  +
  +    strncpy(opts.host, TEST_SERVER, 255);
  +    opts.host[254] = '\0';
  +    opts.port = 27017;
  +
  +    if (mongo_connect( conn , &opts )){
  +        printf("failed to connect\n");
  +        exit(1);
  +    }
  +
  +    mongo_cmd_drop_db(conn, db);
  +
  +    ASSERT(mongo_cmd_authenticate(conn, db, "user", "password") == 0);
  +    mongo_cmd_add_user(conn, db, "user", "password");
  +    ASSERT(mongo_cmd_authenticate(conn, db, "user", "password") == 1);
  +
  +    return 0;
  +}
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/tests/mongo/benchmark.c
  ============================================================================
  $ cvs diff -u -r0 -r1.1 benchmark.c
  --- /dev/null	2010-09-17 22:33:42.000000000 +0200
  +++ benchmark.c	2010-09-17 22:34:09.344657515 +0200
  @@ -0,0 +1,434 @@
  +/* test.c */
  +
  +#include "test.h"
  +#include "mongo.h"
  +#include <stdio.h>
  +#include <string.h>
  +#include <stdlib.h>
  +
  +#ifndef _WIN32
  +#include <sys/time.h>
  +#endif
  +
  +/* supports preprocessor concatenation */
  +#define DB "benchmarks"
  +
  +/* finds without indexes */
  +#define DO_SLOW_TESTS 1
  +
  +#ifndef TEST_SERVER
  +#define TEST_SERVER "127.0.0.1"
  +#endif
  +
  +#define PER_TRIAL 5000
  +#define BATCH_SIZE  100
  +
  +static mongo_connection conn[1];
  +
  +static void make_small(bson * out, int i){
  +    bson_buffer bb;
  +    bson_buffer_init(&bb);
  +    bson_append_new_oid(&bb, "_id");
  +    bson_append_int(&bb, "x", i);
  +    bson_from_buffer(out, &bb);
  +}
  +
  +static void make_medium(bson * out, int i){
  +    bson_buffer bb;
  +    bson_buffer_init(&bb);
  +    bson_append_new_oid(&bb, "_id");
  +    bson_append_int(&bb, "x", i);
  +    bson_append_int(&bb, "integer", 5);
  +    bson_append_double(&bb, "number", 5.05);
  +    bson_append_bool(&bb, "boolean", 0);
  +
  +    bson_append_start_array(&bb, "array");
  +    bson_append_string(&bb, "0", "test");
  +    bson_append_string(&bb, "1", "benchmark");
  +    bson_append_finish_object(&bb);
  +
  +    bson_from_buffer(out, &bb);
  +}
  +
  +static const char *words[14] = 
  +    {"10gen","web","open","source","application","paas",
  +    "platform-as-a-service","technology","helps",
  +    "developers","focus","building","mongodb","mongo"};
  +
  +static void make_large(bson * out, int i){
  +    int num;
  +    char numstr[4];
  +    bson_buffer bb;
  +    bson_buffer_init(&bb);
  +
  +    bson_append_new_oid(&bb, "_id");
  +    bson_append_int(&bb, "x", i);
  +    bson_append_string(&bb, "base_url", "http://www.example.com/test-me");
  +    bson_append_int(&bb, "total_word_count", 6743);
  +    bson_append_int(&bb, "access_time", 999); /*TODO use date*/
  +
  +    bson_append_start_object(&bb, "meta_tags");
  +    bson_append_string(&bb, "description", "i am a long description string");
  +    bson_append_string(&bb, "author", "Holly Man");
  +    bson_append_string(&bb, "dynamically_created_meta_tag", "who know\n what");
  +    bson_append_finish_object(&bb);
  +
  +    bson_append_start_object(&bb, "page_structure");
  +    bson_append_int(&bb, "counted_tags", 3450);
  +    bson_append_int(&bb, "no_of_js_attached", 10);
  +    bson_append_int(&bb, "no_of_images", 6);
  +    bson_append_finish_object(&bb);
  +
  +
  +    bson_append_start_array(&bb, "harvested_words");
  +    for (num=0; num < 14*20; num++){
  +        bson_numstr(numstr, num);
  +        bson_append_string(&bb, numstr, words[num%14]);
  +    }
  +    bson_append_finish_object(&bb);
  +
  +    bson_from_buffer(out, &bb);
  +}
  +
  +static void serialize_small_test(){
  +    int i;
  +    bson b;
  +    for (i=0; i<PER_TRIAL; i++){
  +        make_small(&b, i);
  +        bson_destroy(&b);
  +    }
  +}
  +static void serialize_medium_test(){
  +    int i;
  +    bson b;
  +    for (i=0; i<PER_TRIAL; i++){
  +        make_medium(&b, i);
  +        bson_destroy(&b);
  +    }
  +}
  +static void serialize_large_test(){
  +    int i;
  +    bson b;
  +    for (i=0; i<PER_TRIAL; i++){
  +        make_large(&b, i);
  +        bson_destroy(&b);
  +    }
  +}
  +static void single_insert_small_test(){
  +    int i;
  +    bson b;
  +    for (i=0; i<PER_TRIAL; i++){
  +        make_small(&b, i);
  +        mongo_insert(conn, DB ".single.small", &b);
  +        bson_destroy(&b);
  +    }
  +}
  +
  +static void single_insert_medium_test(){
  +    int i;
  +    bson b;
  +    for (i=0; i<PER_TRIAL; i++){
  +        make_medium(&b, i);
  +        mongo_insert(conn, DB ".single.medium", &b);
  +        bson_destroy(&b);
  +    }
  +}
  +
  +static void single_insert_large_test(){
  +    int i;
  +    bson b;
  +    for (i=0; i<PER_TRIAL; i++){
  +        make_large(&b, i);
  +        mongo_insert(conn, DB ".single.large", &b);
  +        bson_destroy(&b);
  +    }
  +}
  +
  +static void index_insert_small_test(){
  +    int i;
  +    bson b;
  +    ASSERT(mongo_create_simple_index(conn, DB ".index.small", "x", 0, NULL));
  +    for (i=0; i<PER_TRIAL; i++){
  +        make_small(&b, i);
  +        mongo_insert(conn, DB ".index.small", &b);
  +        bson_destroy(&b);
  +    }
  +}
  +
  +static void index_insert_medium_test(){
  +    int i;
  +    bson b;
  +    ASSERT(mongo_create_simple_index(conn, DB ".index.medium", "x", 0, NULL));
  +    for (i=0; i<PER_TRIAL; i++){
  +        make_medium(&b, i);
  +        mongo_insert(conn, DB ".index.medium", &b);
  +        bson_destroy(&b);
  +    }
  +}
  +
  +static void index_insert_large_test(){
  +    int i;
  +    bson b;
  +    ASSERT(mongo_create_simple_index(conn, DB ".index.large", "x", 0, NULL));
  +    for (i=0; i<PER_TRIAL; i++){
  +        make_large(&b, i);
  +        mongo_insert(conn, DB ".index.large", &b);
  +        bson_destroy(&b);
  +    }
  +}
  +
  +static void batch_insert_small_test(){
  +    int i, j;
  +    bson b[BATCH_SIZE];
  +    bson *bp[BATCH_SIZE];
  +    for (j=0; j < BATCH_SIZE; j++)
  +        bp[j] = &b[j];
  +
  +    for (i=0; i < (PER_TRIAL / BATCH_SIZE); i++){
  +        for (j=0; j < BATCH_SIZE; j++)
  +            make_small(&b[j], i);
  +
  +        mongo_insert_batch(conn, DB ".batch.small", bp, BATCH_SIZE);
  +
  +        for (j=0; j < BATCH_SIZE; j++)
  +            bson_destroy(&b[j]);
  +    }
  +}
  +
  +static void batch_insert_medium_test(){
  +    int i, j;
  +    bson b[BATCH_SIZE];
  +    bson *bp[BATCH_SIZE];
  +    for (j=0; j < BATCH_SIZE; j++)
  +        bp[j] = &b[j];
  +
  +    for (i=0; i < (PER_TRIAL / BATCH_SIZE); i++){
  +        for (j=0; j < BATCH_SIZE; j++)
  +            make_medium(&b[j], i);
  +
  +        mongo_insert_batch(conn, DB ".batch.medium", bp, BATCH_SIZE);
  +
  +        for (j=0; j < BATCH_SIZE; j++)
  +            bson_destroy(&b[j]);
  +    }
  +}
  +
  +static void batch_insert_large_test(){
  +    int i, j;
  +    bson b[BATCH_SIZE];
  +    bson *bp[BATCH_SIZE];
  +    for (j=0; j < BATCH_SIZE; j++)
  +        bp[j] = &b[j];
  +
  +    for (i=0; i < (PER_TRIAL / BATCH_SIZE); i++){
  +        for (j=0; j < BATCH_SIZE; j++)
  +            make_large(&b[j], i);
  +
  +        mongo_insert_batch(conn, DB ".batch.large", bp, BATCH_SIZE);
  +
  +        for (j=0; j < BATCH_SIZE; j++)
  +            bson_destroy(&b[j]);
  +    }
  +}
  +
  +static void make_query(bson* b){
  +    bson_buffer bb;
  +    bson_buffer_init(&bb);
  +    bson_append_int(&bb, "x", PER_TRIAL/2);
  +    bson_from_buffer(b, &bb);
  +}
  +
  +static void find_one(const char* ns){
  +    bson b;
  +    int i;
  +    for (i=0; i < PER_TRIAL; i++){
  +        make_query(&b);
  +        ASSERT(mongo_find_one(conn, ns, &b, NULL, NULL));
  +        bson_destroy(&b);
  +    }
  +}
  +
  +static void find_one_noindex_small_test()  {find_one(DB ".single.small");}
  +static void find_one_noindex_medium_test() {find_one(DB ".single.medium");}
  +static void find_one_noindex_large_test()  {find_one(DB ".single.large");}
  +
  +static void find_one_index_small_test()  {find_one(DB ".index.small");}
  +static void find_one_index_medium_test() {find_one(DB ".index.medium");}
  +static void find_one_index_large_test()  {find_one(DB ".index.large");}
  +
  +static void find(const char* ns){
  +    bson b;
  +    int i;
  +    for (i=0; i < PER_TRIAL; i++){
  +        mongo_cursor * cursor;
  +        make_query(&b);
  +        cursor = mongo_find(conn, ns, &b, NULL, 0,0,0);
  +        ASSERT(cursor);
  +
  +        while(mongo_cursor_next(cursor))
  +        {}
  +
  +        mongo_cursor_destroy(cursor);
  +        bson_destroy(&b);
  +    }
  +}
  +
  +static void find_noindex_small_test()  {find(DB ".single.small");}
  +static void find_noindex_medium_test() {find(DB ".single.medium");}
  +static void find_noindex_large_test()  {find(DB ".single.large");}
  +
  +static void find_index_small_test()  {find(DB ".index.small");}
  +static void find_index_medium_test() {find(DB ".index.medium");}
  +static void find_index_large_test()  {find(DB ".index.large");}
  +
  +
  +static void find_range(const char* ns){
  +    int i;
  +    bson b;
  +
  +    for (i=0; i < PER_TRIAL; i++){
  +        int j=0;
  +        mongo_cursor * cursor;
  +        bson_buffer bb;
  +
  +        bson_buffer_init(&bb);
  +        bson_append_start_object(&bb, "x");
  +        bson_append_int(&bb, "$gt", PER_TRIAL/2);
  +        bson_append_int(&bb, "$lt", PER_TRIAL/2 + BATCH_SIZE);
  +        bson_append_finish_object(&bb);
  +        bson_from_buffer(&b, &bb);
  +
  +        cursor = mongo_find(conn, ns, &b, NULL, 0,0,0);
  +        ASSERT(cursor);
  +
  +        while(mongo_cursor_next(cursor)) {
  +            j++;
  +        }
  +        ASSERT(j == BATCH_SIZE-1);
  +
  +        mongo_cursor_destroy(cursor);
  +        bson_destroy(&b);
  +    }
  +}
  +
  +static void find_range_small_test()  {find_range(DB ".index.small");}
  +static void find_range_medium_test() {find_range(DB ".index.medium");}
  +static void find_range_large_test()  {find_range(DB ".index.large");}
  +
  +typedef void(*nullary)();
  +static void time_it(nullary func, const char* name, bson_bool_t gle){
  +    double timer;
  +    double ops;
  +
  +#ifdef _WIN32
  +    int64_t start, end;
  +
  +    start = GetTickCount64();
  +    func();
  +    if (gle) ASSERT(!mongo_cmd_get_last_error(conn, DB, NULL));
  +    end = GetTickCount64();
  +
  +    timer = end - start;
  +#else
  +    struct timeval start, end;
  +
  +    gettimeofday(&start, NULL);
  +    func();
  +    if (gle) ASSERT(!mongo_cmd_get_last_error(conn, DB, NULL));
  +    gettimeofday(&end, NULL);
  +
  +    timer = end.tv_sec - start.tv_sec;
  +    timer *= 1000000;
  +    timer += end.tv_usec - start.tv_usec;
  +#endif
  +
  +    ops = PER_TRIAL / timer;
  +    ops *= 1000000;
  +
  +    printf("%-45s\t%15f\n", name, ops);
  +}
  +
  +#define TIME(func, gle) (time_it(func, #func, gle))
  +
  +static void clean(){
  +    bson b;
  +    if (!mongo_cmd_drop_db(conn, DB)){
  +        printf("failed to drop db\n");
  +        exit(1);
  +    }
  +
  +    /* create the db */
  +    mongo_insert(conn, DB ".creation", bson_empty(&b));
  +    ASSERT(!mongo_cmd_get_last_error(conn, DB, NULL));
  +}
  +
  +int main(){
  +    mongo_connection_options opts;
  +
  +    INIT_SOCKETS_FOR_WINDOWS;
  +
  +    strncpy(opts.host, TEST_SERVER, 255);
  +    opts.host[254] = '\0';
  +    opts.port = 27017;
  +
  +    if (mongo_connect(conn, &opts )){
  +        printf("failed to connect\n");
  +        exit(1);
  +    }
  +
  +    clean();
  +
  +    printf("-----\n");
  +    TIME(serialize_small_test, 0);
  +    TIME(serialize_medium_test, 0);
  +    TIME(serialize_large_test, 0);
  +
  +    printf("-----\n");
  +    TIME(single_insert_small_test, 1);
  +    TIME(single_insert_medium_test, 1);
  +    TIME(single_insert_large_test, 1);
  +
  +    printf("-----\n");
  +    TIME(index_insert_small_test, 1);
  +    TIME(index_insert_medium_test, 1);
  +    TIME(index_insert_large_test, 1);
  +
  +    printf("-----\n");
  +    TIME(batch_insert_small_test, 1);
  +    TIME(batch_insert_medium_test, 1);
  +    TIME(batch_insert_large_test, 1);
  +
  +#if DO_SLOW_TESTS
  +    printf("-----\n");
  +    TIME(find_one_noindex_small_test, 0);
  +    TIME(find_one_noindex_medium_test, 0);
  +    TIME(find_one_noindex_large_test, 0);
  +#endif
  +
  +    printf("-----\n");
  +    TIME(find_one_index_small_test, 0);
  +    TIME(find_one_index_medium_test, 0);
  +    TIME(find_one_index_large_test, 0);
  +
  +#if DO_SLOW_TESTS
  +    printf("-----\n");
  +    TIME(find_noindex_small_test, 0);
  +    TIME(find_noindex_medium_test, 0);
  +    TIME(find_noindex_large_test, 0);
  +#endif
  +
  +    printf("-----\n");
  +    TIME(find_index_small_test, 0);
  +    TIME(find_index_medium_test, 0);
  +    TIME(find_index_large_test, 0);
  +
  +    printf("-----\n");
  +    TIME(find_range_small_test, 0);
  +    TIME(find_range_medium_test, 0);
  +    TIME(find_range_large_test, 0);
  +
  +
  +    mongo_destroy(conn);
  +
  +    return 0;
  +}
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/tests/mongo/count_delete.c
  ============================================================================
  $ cvs diff -u -r0 -r1.1 count_delete.c
  --- /dev/null	2010-09-17 22:33:42.000000000 +0200
  +++ count_delete.c	2010-09-17 22:34:09.384659467 +0200
  @@ -0,0 +1,68 @@
  +/* count_delete.c */
  +
  +#include "test.h"
  +#include "mongo.h"
  +#include <stdio.h>
  +#include <string.h>
  +#include <stdlib.h>
  +
  +int main(){
  +    mongo_connection conn[1];
  +    mongo_connection_options opts;
  +    bson_buffer bb;
  +    bson b;
  +    int i;
  +
  +    const char * db = "test";
  +    const char * col = "c.simple";
  +    const char * ns = "test.c.simple";
  +
  +    INIT_SOCKETS_FOR_WINDOWS;
  +    
  +    strncpy(opts.host, TEST_SERVER, 255);
  +    opts.host[254] = '\0';
  +    opts.port = 27017;
  +
  +    if (mongo_connect( conn , &opts )){
  +        printf("failed to connect\n");
  +        exit(1);
  +    }
  +
  +    /* if the collection doesn't exist dropping it will fail */
  +    if (!mongo_cmd_drop_collection(conn, "test", col, NULL)
  +          && mongo_count(conn, db, col, NULL) != 0){
  +        printf("failed to drop collection\n");
  +        exit(1);
  +    }
  +
  +    for(i=0; i< 5; i++){
  +        bson_buffer_init( & bb );
  +
  +        bson_append_new_oid( &bb, "_id" );
  +        bson_append_int( &bb , "a" , i+1 ); /* 1 to 5 */
  +        bson_from_buffer(&b, &bb);
  +
  +        mongo_insert( conn , ns , &b );
  +        bson_destroy(&b);
  +    }
  +
  +    /* query: {a: {$gt: 3}} */
  +    bson_buffer_init( & bb );
  +    {
  +        bson_buffer * sub = bson_append_start_object(&bb, "a");
  +        bson_append_int(sub, "$gt", 3);
  +        bson_append_finish_object(sub);
  +    }
  +    bson_from_buffer(&b, &bb);
  +    
  +    ASSERT(mongo_count(conn, db, col, NULL) == 5);
  +    ASSERT(mongo_count(conn, db, col, &b) == 2);
  +
  +    mongo_remove(conn, ns, &b);
  +
  +    ASSERT(mongo_count(conn, db, col, NULL) == 3);
  +    ASSERT(mongo_count(conn, db, col, &b) == 0);
  +
  +    mongo_destroy( conn );
  +    return 0;
  +}
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/tests/mongo/endian_swap.c
  ============================================================================
  $ cvs diff -u -r0 -r1.1 endian_swap.c
  --- /dev/null	2010-09-17 22:33:42.000000000 +0200
  +++ endian_swap.c	2010-09-17 22:34:09.414661350 +0200
  @@ -0,0 +1,31 @@
  +/* endian_swap.c */
  +
  +#include "test.h"
  +#include "platform_hacks.h"
  +#include <stdio.h>
  +
  +int main(){
  +    int small = 0x00112233;
  +    int64_t big = 0x0011223344556677;
  +    double d = 1.2345;
  +
  +    int small_swap;
  +    int64_t big_swap;
  +    int64_t d_swap;
  +
  +    bson_swap_endian32(&small_swap, &small);
  +    ASSERT( small_swap == 0x33221100);
  +    bson_swap_endian32(&small, &small_swap);
  +    ASSERT(small == 0x00112233);
  +
  +    bson_swap_endian64(&big_swap, &big);
  +    ASSERT(big_swap == 0x7766554433221100);
  +    bson_swap_endian64(&big, &big_swap);
  +    ASSERT(big == 0x0011223344556677);
  +
  +    bson_swap_endian64(&d_swap, &d);
  +    bson_swap_endian64(&d, &d_swap);
  +    ASSERT(d == 1.2345);
  +
  +    return 0;
  +}
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/tests/mongo/errors.c
  ============================================================================
  $ cvs diff -u -r0 -r1.1 errors.c
  --- /dev/null	2010-09-17 22:33:42.000000000 +0200
  +++ errors.c	2010-09-17 22:34:09.444662953 +0200
  @@ -0,0 +1,74 @@
  +#include "test.h"
  +#include "mongo.h"
  +#include <stdio.h>
  +#include <string.h>
  +#include <stdlib.h>
  +
  +static mongo_connection conn[1];
  +static mongo_connection_options opts;
  +static const char* db = "test";
  +static const char* ns = "test.c.error";
  +
  +int main(){
  +    bson obj;
  +
  +    INIT_SOCKETS_FOR_WINDOWS;
  +
  +    strncpy(opts.host, TEST_SERVER, 255);
  +    opts.host[254] = '\0';
  +    opts.port = 27017;
  +
  +    if (mongo_connect( conn , &opts )){
  +        printf("failed to connect\n");
  +        exit(1);
  +    }
  +
  +
  +    /*********************/
  +    ASSERT(!mongo_cmd_get_prev_error(conn, db, NULL));
  +    ASSERT(!mongo_cmd_get_last_error(conn, db, NULL));
  +
  +    ASSERT(!mongo_cmd_get_prev_error(conn, db, &obj));
  +    bson_destroy(&obj);
  +
  +    ASSERT(!mongo_cmd_get_last_error(conn, db, &obj));
  +    bson_destroy(&obj);
  +
  +    /*********************/
  +    mongo_simple_int_command(conn, db, "forceerror", 1, NULL);
  +
  +    ASSERT(mongo_cmd_get_prev_error(conn, db, NULL));
  +    ASSERT(mongo_cmd_get_last_error(conn, db, NULL));
  +
  +    ASSERT(mongo_cmd_get_prev_error(conn, db, &obj));
  +    bson_destroy(&obj);
  +
  +    ASSERT(mongo_cmd_get_last_error(conn, db, &obj));
  +    bson_destroy(&obj);
  +
  +    /* should clear lasterror but not preverror */
  +    mongo_find_one(conn, ns, bson_empty(&obj), bson_empty(&obj), NULL);
  +
  +    ASSERT(mongo_cmd_get_prev_error(conn, db, NULL));
  +    ASSERT(!mongo_cmd_get_last_error(conn, db, NULL));
  +
  +    ASSERT(mongo_cmd_get_prev_error(conn, db, &obj));
  +    bson_destroy(&obj);
  +
  +    ASSERT(!mongo_cmd_get_last_error(conn, db, &obj));
  +    bson_destroy(&obj);
  +
  +    /*********************/
  +    mongo_cmd_reset_error(conn, db);
  +
  +    ASSERT(!mongo_cmd_get_prev_error(conn, db, NULL));
  +    ASSERT(!mongo_cmd_get_last_error(conn, db, NULL));
  +
  +    ASSERT(!mongo_cmd_get_prev_error(conn, db, &obj));
  +    bson_destroy(&obj);
  +
  +    ASSERT(!mongo_cmd_get_last_error(conn, db, &obj));
  +    bson_destroy(&obj);
  +
  +    return 0;
  +}
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/tests/mongo/json.c
  ============================================================================
  $ cvs diff -u -r0 -r1.1 json.c
  --- /dev/null	2010-09-17 22:33:42.000000000 +0200
  +++ json.c	2010-09-17 22:34:09.474664277 +0200
  @@ -0,0 +1,169 @@
  +/* testjson.c */
  +
  +#include "test.h"
  +#include <stdio.h>
  +#include <string.h>
  +
  +#include "mongo.h"
  +#include "json/json.h"
  +#include "md5.h"
  +
  +void json_to_bson_append_element( bson_buffer * bb , const char * k , struct json_object * v );
  +
  +/**
  +   should already have called start_array
  +   this will not call start/finish
  + */
  +void json_to_bson_append_array( bson_buffer * bb , struct json_object * a ){
  +    int i;
  +    char buf[10];
  +    for ( i=0; i<json_object_array_length( a ); i++){
  +        sprintf( buf , "%d" , i );
  +        json_to_bson_append_element( bb , buf , json_object_array_get_idx( a , i ) );
  +    }
  +}
  +
  +void json_to_bson_append( bson_buffer * bb , struct json_object * o ){
  +    json_object_object_foreach(o,k,v){
  +        json_to_bson_append_element( bb , k , v );
  +    }
  +}
  +
  +void json_to_bson_append_element( bson_buffer * bb , const char * k , struct json_object * v ){
  +    if ( ! v ){
  +        bson_append_null( bb , k );
  +        return;
  +    }
  +    
  +    switch ( json_object_get_type( v ) ){
  +    case json_type_int:
  +        bson_append_int( bb , k , json_object_get_int( v ) );
  +        break;
  +    case json_type_boolean:
  +        bson_append_bool( bb , k , json_object_get_boolean( v ) );
  +        break;
  +    case json_type_double:
  +        bson_append_double( bb , k , json_object_get_double( v ) );
  +        break;
  +    case json_type_string:
  +        bson_append_string( bb , k , json_object_get_string( v ) );
  +        break;
  +    case json_type_object:
  +        bson_append_start_object( bb , k );
  +        json_to_bson_append( bb , v );
  +        bson_append_finish_object( bb );
  +        break;
  +    case json_type_array:
  +        bson_append_start_array( bb , k );
  +        json_to_bson_append_array( bb , v );
  +        bson_append_finish_object( bb );
  +        break;
  +    default:
  +        fprintf( stderr , "can't handle type for : %s\n" , json_object_to_json_string(v) );
  +    }
  +}
  +
  +
  +char * json_to_bson( char * js ){
  +    struct json_object * o = json_tokener_parse(js);
  +    bson_buffer bb;
  +    
  +    if ( is_error( o ) ){
  +        fprintf( stderr , "\t ERROR PARSING\n" );
  +        return 0;
  +    }
  +    
  +    if ( ! json_object_is_type( o , json_type_object ) ){
  +        fprintf( stderr , "json_to_bson needs a JSON object, not type\n" );
  +        return 0;
  +    }
  +    
  +    bson_buffer_init( &bb );
  +    json_to_bson_append( &bb , o );
  +    return bson_buffer_finish( &bb );
  +}
  +
  +int json_to_bson_test( char * js , int size , const char * hash ){
  +    bson b;
  +    mongo_md5_state_t st;
  +    mongo_md5_byte_t digest[16];
  +    char myhash[33];
  +    int i;
  +
  +    fprintf( stderr , "----\n%s\n" , js );
  +
  +    
  +    bson_init( &b , json_to_bson( js ) , 1 );
  +
  +    if ( b.data == 0 ){
  +        if ( size == 0 )
  +            return 1;
  +        fprintf( stderr , "failed when wasn't supposed to: %s\n" , js );
  +        return 0;
  +        
  +    }
  +    
  +    if ( size != bson_size( &b ) ){
  +        fprintf( stderr , "sizes don't match [%s] want != got %d != %d\n" , js , size , bson_size(&b) );
  +        bson_destroy( &b );
  +        return 0;
  +    }    
  +
  +    mongo_md5_init(&st);
  +    mongo_md5_append( &st , (const mongo_md5_byte_t*)b.data , bson_size( &b ) );
  +    mongo_md5_finish(&st, digest);
  +
  +    for ( i=0; i<16; i++ )
  +        sprintf( myhash + ( i * 2 ) , "%.2x" , digest[i] );
  +    myhash[32] = 0;
  +
  +    if ( strlen( hash ) != 32 ){
  +        printf( "\tinvalid hash given got %s\n" , myhash );
  +        bson_destroy( &b );
  +        return 0;
  +    }
  +    else if ( strstr( myhash , hash ) != myhash ){
  +        printf( "  hashes don't match\n");
  +        printf( "    JSON:  %s\n\t%s\n", hash, js );
  +        printf( "    BSON:  %s\n", myhash);
  +        bson_print( &b );
  +        bson_destroy( &b );
  +        return 0;
  +    }
  +
  +    bson_destroy( &b );
  +    return 1;
  +}
  +
  +int total = 0;
  +int fails = 0;
  +
  +int run_json_to_bson_test( char * js , int size , const char * hash ){
  +    total++;
  +    if ( ! json_to_bson_test( js , size , hash ) )
  +        fails++;
  +    
  +    return fails;
  +}
  +
  +#define JSONBSONTEST run_json_to_bson_test
  +
  +int main(){
  +
  +    run_json_to_bson_test( "1" , 0 , 0 );
  +    
  +    JSONBSONTEST( "{ 'x' : true }" , 9 , "6fe24623e4efc5cf07f027f9c66b5456" );
  +    JSONBSONTEST( "{ 'x' : null }" , 8 , "12d43430ff6729af501faf0638e68888" );
  +    JSONBSONTEST( "{ 'x' : 5.2 }" , 16 , "aaeeac4a58e9c30eec6b0b0319d0dff2" );
  +    JSONBSONTEST( "{ 'x' : 'eliot' }" , 18 , "331a3b8b7cbbe0706c80acdb45d4ebbe" );
  +    JSONBSONTEST( "{ 'x' : 5.2 , 'y' : 'truth' , 'z' : 1.1 }" , 40 , "7c77b3a6e63e2f988ede92624409da58" );
  +    JSONBSONTEST( "{ 'x' : 4 }" , 12 , "d1ed8dbf79b78fa215e2ded74548d89d" );
  +    JSONBSONTEST( "{ 'x' : 5.2 , 'y' : 'truth' , 'z' : 1 }" , 36 , "8993953de080e9d4ef449d18211ef88a" );
  +    JSONBSONTEST( "{ 'x' : 'eliot' , 'y' : true , 'z' : 1 }" , 29 , "24e79c12e6c746966b123310cb1a3290" );
  +    JSONBSONTEST( "{ 'a' : { 'b' : 1.1 } }" , 24 , "31887a4b9d55cd9f17752d6a8a45d51f" );
  +    JSONBSONTEST( "{ 'x' : 5.2 , 'y' : { 'a' : 'eliot' , 'b' : true } , 'z' : null }" , 44 , "b3de8a0739ab329e7aea138d87235205" );
  +    JSONBSONTEST( "{ 'x' : 5.2 , 'y' : [ 'a' , 'eliot' , 'b' , true ] , 'z' : null }" , 62 , "cb7bad5697714ba0cbf51d113b6a0ee8" );
  +
  +    fprintf( stderr,  "----\ntotal: %d\nfails : %d\n" , total , fails );
  +    return fails;
  +}
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/tests/mongo/pair.c
  ============================================================================
  $ cvs diff -u -r0 -r1.1 pair.c
  --- /dev/null	2010-09-17 22:33:42.000000000 +0200
  +++ pair.c	2010-09-17 22:34:09.504666439 +0200
  @@ -0,0 +1,41 @@
  +#include "test.h"
  +#include "mongo.h"
  +#include <stdio.h>
  +#include <string.h>
  +#include <stdlib.h>
  +
  +static mongo_connection conn[1];
  +static mongo_connection_options left;
  +static mongo_connection_options right;
  +
  +int main(){
  +
  +    INIT_SOCKETS_FOR_WINDOWS;
  +
  +    strncpy(left.host, TEST_SERVER, 255);
  +    left.host[254] = '\0';
  +    left.port = 27017;
  +
  +    strncpy(right.host, "0.0.0.0", 255);
  +    right.port = 12345;
  +
  +    ASSERT(mongo_connect_pair(conn, &left, &right) == mongo_conn_success);
  +    ASSERT(conn->left_opts->port == 27017);
  +    ASSERT(conn->right_opts->port == 12345);
  +    ASSERT(mongo_cmd_ismaster(conn, NULL));
  +
  +    mongo_destroy(conn);
  +
  +    ASSERT(mongo_connect_pair(conn, &right, &left) == mongo_conn_success);
  +    ASSERT(conn->left_opts->port == 27017); /* should have swapped left and right */
  +    ASSERT(conn->right_opts->port == 12345);
  +    ASSERT(mongo_cmd_ismaster(conn, NULL));
  +
  +    ASSERT(mongo_reconnect(conn) == mongo_conn_success);
  +    ASSERT(conn->left_opts->port == 27017); /* should have swapped left and right */
  +    ASSERT(conn->right_opts->port == 12345);
  +    ASSERT(mongo_cmd_ismaster(conn, NULL));
  +
  +    mongo_destroy(conn);
  +    return 0;
  +}
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/tests/mongo/resize.c
  ============================================================================
  $ cvs diff -u -r0 -r1.1 resize.c
  --- /dev/null	2010-09-17 22:33:42.000000000 +0200
  +++ resize.c	2010-09-17 22:34:09.534667484 +0200
  @@ -0,0 +1,35 @@
  +/* resize.c */
  +
  +#include "test.h"
  +#include "bson.h"
  +#include <string.h>
  +
  +/* 64 Xs */
  +const char* bigstring = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  +
  +int main(){
  +    bson_buffer bb;
  +    bson b;
  +
  +    bson_buffer_init(&bb);
  +    bson_append_string(&bb, "a", bigstring);
  +    bson_append_start_object(&bb, "sub");
  +        bson_append_string(&bb,"a", bigstring);
  +        bson_append_start_object(&bb, "sub");
  +            bson_append_string(&bb,"a", bigstring);
  +            bson_append_start_object(&bb, "sub");
  +                bson_append_string(&bb,"a", bigstring);
  +                bson_append_string(&bb,"b", bigstring);
  +                bson_append_string(&bb,"c", bigstring);
  +                bson_append_string(&bb,"d", bigstring);
  +                bson_append_string(&bb,"e", bigstring);
  +                bson_append_string(&bb,"f", bigstring);
  +            bson_append_finish_object(&bb);
  +        bson_append_finish_object(&bb);
  +    bson_append_finish_object(&bb);
  +    bson_from_buffer(&b, &bb);
  +
  +    /* bson_print(&b); */
  +    bson_destroy(&b);
  +    return 0;
  +}
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/tests/mongo/simple.c
  ============================================================================
  $ cvs diff -u -r0 -r1.1 simple.c
  --- /dev/null	2010-09-17 22:33:42.000000000 +0200
  +++ simple.c	2010-09-17 22:34:09.614671667 +0200
  @@ -0,0 +1,103 @@
  +/* test.c */
  +
  +#include "test.h"
  +#include "mongo.h"
  +#include <stdio.h>
  +#include <string.h>
  +#include <stdlib.h>
  +
  +int main(){
  +    mongo_connection conn[1];
  +    mongo_connection_options opts;
  +    bson_buffer bb;
  +    bson b;
  +    mongo_cursor * cursor;
  +    int i;
  +    char hex_oid[25];
  +
  +    const char * col = "c.simple";
  +    const char * ns = "test.c.simple";
  +
  +    INIT_SOCKETS_FOR_WINDOWS;
  +    
  +    strncpy(opts.host, TEST_SERVER, 255);
  +    opts.host[254] = '\0';
  +    opts.port = 27017;
  +
  +    if (mongo_connect( conn , &opts )){
  +        printf("failed to connect\n");
  +        exit(1);
  +    }
  +
  +    /* if the collection doesn't exist dropping it will fail */
  +    if (!mongo_cmd_drop_collection(conn, "test", col, NULL)
  +          && mongo_find_one(conn, ns, bson_empty(&b), bson_empty(&b), NULL)){
  +        printf("failed to drop collection\n");
  +        exit(1);
  +    }
  +
  +    for(i=0; i< 5; i++){
  +        bson_buffer_init( & bb );
  +
  +        bson_append_new_oid( &bb, "_id" );
  +        bson_append_double( &bb , "a" , 17 );
  +        bson_append_int( &bb , "b" , 17 );
  +        bson_append_string( &bb , "c" , "17" );
  +
  +        {
  +            bson_buffer * sub = bson_append_start_object(  &bb , "d" );
  +            bson_append_int( sub, "i", 71 );
  +            bson_append_finish_object(sub);
  +        }
  +        {
  +            bson_buffer * arr = bson_append_start_array(  &bb , "e" );
  +            bson_append_int( arr, "0", 71 );
  +            bson_append_string( arr, "1", "71" );
  +            bson_append_finish_object(arr);
  +        }
  +
  +        bson_from_buffer(&b, &bb);
  +        mongo_insert( conn , ns , &b );
  +        bson_destroy(&b);
  +    }
  +    
  +    cursor = mongo_find( conn , ns , bson_empty(&b) , 0 , 0 , 0 , 0 );
  +
  +    while (mongo_cursor_next(cursor)){
  +        bson_iterator it;
  +        bson_iterator_init(&it, cursor->current.data);
  +        while(bson_iterator_next(&it)){
  +            fprintf(stderr, "  %s: ", bson_iterator_key(&it));
  +
  +            switch(bson_iterator_type(&it)){
  +                case bson_double:
  +                    fprintf(stderr, "(double) %e\n", bson_iterator_double(&it));
  +                    break;
  +                case bson_int:
  +                    fprintf(stderr, "(int) %d\n", bson_iterator_int(&it));
  +                    break;
  +                case bson_string:
  +                    fprintf(stderr, "(string) \"%s\"\n", bson_iterator_string(&it));
  +                    break;
  +                case bson_oid:
  +                    bson_oid_to_string(bson_iterator_oid(&it), hex_oid);
  +                    fprintf(stderr, "(oid) \"%s\"\n", hex_oid);
  +                    break;
  +                case bson_object:
  +                    fprintf(stderr, "(subobject) {...}\n");
  +                    break;
  +                case bson_array:
  +                    fprintf(stderr, "(array) [...]\n");
  +                    break;
  +                default:
  +                    fprintf(stderr, "(type %d)\n", bson_iterator_type(&it));
  +                    break;
  +            }
  +        }
  +        fprintf(stderr, "\n");
  +    }
  +
  +    mongo_cursor_destroy(cursor);
  +    mongo_destroy( conn );
  +    return 0;
  +}
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/tests/mongo/sizes.c
  ============================================================================
  $ cvs diff -u -r0 -r1.1 sizes.c
  --- /dev/null	2010-09-17 22:33:42.000000000 +0200
  +++ sizes.c	2010-09-17 22:34:09.654673618 +0200
  @@ -0,0 +1,22 @@
  +/* sizes.c */
  +
  +#include "test.h"
  +#include "mongo.h"
  +#include <stdio.h>
  +
  +int main(){
  +    mongo_reply mr;
  +
  +    ASSERT(sizeof(int) == 4);
  +    ASSERT(sizeof(int64_t) == 8);
  +    ASSERT(sizeof(double) == 8);
  +    ASSERT(sizeof(bson_oid_t) == 12);
  +
  +    ASSERT(sizeof(mongo_header) == 4+4+4+4);
  +    ASSERT(sizeof(mongo_reply_fields) == 4+8+4+4);
  +
  +    /* field offset of obj in mongo_reply */
  +    ASSERT((&mr.objs - (char*)&mr) == (4+4+4+4 + 4+8+4+4));
  +
  +    return 0;
  +}
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/tests/mongo/update.c
  ============================================================================
  $ cvs diff -u -r0 -r1.1 update.c
  --- /dev/null	2010-09-17 22:33:42.000000000 +0200
  +++ update.c	2010-09-17 22:34:09.684674942 +0200
  @@ -0,0 +1,110 @@
  +#include "test.h"
  +#include "mongo.h"
  +#include <stdio.h>
  +#include <string.h>
  +#include <stdlib.h>
  +
  +int main(){
  +    mongo_connection conn[1];
  +    mongo_connection_options opts;
  +    bson_buffer bb;
  +    bson obj;
  +    bson cond;
  +    int i;
  +    bson_oid_t oid;
  +    const char* col = "c.update_test";
  +    const char* ns = "test.c.update_test";
  +
  +    INIT_SOCKETS_FOR_WINDOWS;
  +
  +    strncpy(opts.host, TEST_SERVER, 255);
  +    opts.host[254] = '\0';
  +    opts.port = 27017;
  +
  +    if (mongo_connect( conn , &opts )){
  +        printf("failed to connect\n");
  +        exit(1);
  +    }
  +
  +    /* if the collection doesn't exist dropping it will fail */
  +    if (!mongo_cmd_drop_collection(conn, "test", col, NULL)
  +          && mongo_find_one(conn, ns, bson_empty(&obj), bson_empty(&obj), NULL)){
  +        printf("failed to drop collection\n");
  +        exit(1);
  +    }
  +
  +    bson_oid_gen(&oid);
  +
  +    { /* insert */
  +        bson_buffer_init(&bb);
  +        bson_append_oid(&bb, "_id", &oid);
  +        bson_append_int(&bb, "a", 3 );
  +        bson_from_buffer(&obj, &bb);
  +        mongo_insert(conn, ns, &obj);
  +        bson_destroy(&obj);
  +    }
  +
  +    { /* insert */
  +        bson op;
  +
  +        bson_buffer_init(&bb);
  +        bson_append_oid(&bb, "_id", &oid);
  +        bson_from_buffer(&cond, &bb);
  +
  +        bson_buffer_init(&bb);
  +        {
  +            bson_buffer * sub = bson_append_start_object(&bb, "$inc");
  +            bson_append_int(sub, "a", 2 );
  +            bson_append_finish_object(sub);
  +        }
  +        {
  +            bson_buffer * sub = bson_append_start_object(&bb, "$set");
  +            bson_append_double(sub, "b", -1.5 );
  +            bson_append_finish_object(sub);
  +        }
  +        bson_from_buffer(&op, &bb);
  +
  +        for (i=0; i<5; i++)
  +            mongo_update(conn, ns, &cond, &op, 0);
  +
  +        /* cond is used later */
  +        bson_destroy(&op);
  +    }
  +    
  +    if(!mongo_find_one(conn, ns, &cond, 0, &obj)){
  +        printf("Failed to find object\n");
  +        exit(1);
  +    } else {
  +        int fields = 0;
  +        bson_iterator it;
  +        bson_iterator_init(&it, obj.data);
  +
  +        bson_destroy(&cond);
  +
  +        while(bson_iterator_next(&it)){
  +            switch(bson_iterator_key(&it)[0]){
  +                case '_': /* id */
  +                    ASSERT(bson_iterator_type(&it) == bson_oid);
  +                    ASSERT(!memcmp(bson_iterator_oid(&it)->bytes, oid.bytes, 12));
  +                    fields++;
  +                    break;
  +                case 'a':
  +                    ASSERT(bson_iterator_type(&it) == bson_int);
  +                    ASSERT(bson_iterator_int(&it) == 3 + 5*2);
  +                    fields++;
  +                    break;
  +                case 'b':
  +                    ASSERT(bson_iterator_type(&it) == bson_double);
  +                    ASSERT(bson_iterator_double(&it) == -1.5);
  +                    fields++;
  +                    break;
  +            }
  +        }
  +
  +        ASSERT(fields == 3);
  +    }
  +
  +    bson_destroy(&obj);
  +
  +    return 0;
  +}
  @@ .
Received on Fri Sep 17 22:34:09 2010
Driven by Jeff Johnson and the RPM project team.
Hosted by OpenPKG and Ralf S. Engelschall.
Powered by FreeBSD and OpenPKG.