]> git.jsancho.org Git - bongodb.git/blob - src/test_bongodb.c
Replace VHashes with Hash Tables
[bongodb.git] / src / test_bongodb.c
1 /*
2  * BongoDB, an embedded document-based engine
3  * Copyright (C) 2015 by Javier Sancho Fernandez <jsf at jsancho dot org>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "bongodb.h"
20 #include "jsmn.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 int
27 test_bongodb()
28 {
29   bongo_client_t *bongo_client;
30   bongo_database_t *database;
31   bongo_collection_t *collection;
32   const char *document = "{\"name\": \"Jack\", \"age\": 27, \"list\": [1, 2, 3]}";
33   bongo_error_t error;
34   
35   bongo_client = bongo_client_new ();
36   printf ("Databases: %d\n", bongo_client->n_databases);
37   database = bongo_client_get_database (bongo_client, "db_test");
38   printf ("Databases: %d\n", bongo_client->n_databases);
39
40   printf ("Collections: %d\n", database->database_node->n_collections);
41   collection = bongo_client_get_collection (bongo_client, "db_test", "col_test");
42   printf ("Collections: %d\n", database->database_node->n_collections);
43
44   if (!bongo_collection_insert (collection, BONGO_INSERT_NONE, document, NULL, &error)) {
45     fprintf (stderr, "%s\n", error.message);
46   }
47
48   bongo_collection_destroy (collection);
49   bongo_database_destroy (database);
50   bongo_client_destroy (bongo_client);
51   
52   printf ("test_bongodb: PASSED\n");
53   return 0;
54 }
55
56 int
57 test_jsmn()
58 {
59   jsmn_parser parser;
60   jsmntok_t *tokens;
61   int n_tokens;
62   const char *js = "{\"name\": \"Jack\", \"age\": 27, \"list\": [1, 2, 3]}";
63
64   jsmn_init (&parser);
65   n_tokens = jsmn_parse (&parser, js, strlen (js), NULL, 1);
66   tokens = malloc (sizeof (jsmntok_t) * n_tokens);
67   jsmn_init (&parser);
68   n_tokens = jsmn_parse (&parser, js, strlen (js), tokens, n_tokens);
69   printf ("Tokens: %d\n", n_tokens);
70
71   for (int i = 0; i < n_tokens; i++)
72     {
73       printf ("   %d, %d, %d, %d: %.*s\n", tokens[i].type, tokens[i].start, tokens[i].end, tokens[i].size, tokens[i].end - tokens[i].start, js + tokens[i].start);
74     }
75
76   free (tokens);
77   
78   printf ("test_jsmn: PASSED\n");
79   return 0;
80 }
81
82 int
83 main()
84 {
85   test_bongodb ();
86   test_jsmn ();
87   return 0;
88 }