]> git.jsancho.org Git - bongodb.git/blob - src/test_bongodb.c
24b7cfaeeb8e73da8c9af33c37002d53f2fa598d
[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 test_bongodb()
27 {
28   bongo_client_t *bongo_client;
29   bongo_database_t *bongo_database;
30   bongo_collection_t *bongo_collection;
31   
32   bongo_client = bongo_client_new();
33   printf("Databases: %d\n", bongo_client->n_databases);
34   bongo_database = bongo_client_get_database(bongo_client, "db_test");
35   printf("Databases: %d\n", bongo_client->n_databases);
36
37   printf("Collections: %d\n", bongo_database->database_node->n_collections);
38   bongo_collection = bongo_client_get_collection(bongo_client, "db_test", "col_test");
39   printf("Collections: %d\n", bongo_database->database_node->n_collections);
40
41   bongo_collection_destroy(bongo_collection);
42   bongo_database_destroy(bongo_database);
43   bongo_client_destroy(bongo_client);
44   
45   printf("test_bongodb: PASSED\n");
46   return 0;
47 }
48
49 int test_jsmn()
50 {
51   jsmn_parser parser;
52   jsmntok_t *tokens;
53   int n_tokens;
54   const char *js = "{\"name\": \"Jack\", \"age\": 27, \"list\": [1, 2, 3]}";
55
56   jsmn_init(&parser);
57   n_tokens = jsmn_parse(&parser, js, strlen(js), NULL, 1);
58   tokens = malloc(sizeof(jsmntok_t) * n_tokens);
59   jsmn_init(&parser);
60   n_tokens = jsmn_parse(&parser, js, strlen(js), tokens, n_tokens);
61   printf("Tokens: %d\n", n_tokens);
62
63   for (int i = 0; i < n_tokens; i++)
64     {
65       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);
66     }
67
68   free(tokens);
69   
70   printf("test_jsmn: PASSED\n");
71   return 0;
72 }
73
74 int main()
75 {
76   test_bongodb();
77   test_jsmn();
78   return 0;
79 }