1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include "json.h"
#include "string.h"

// okay to copy/adapt

int _test_get_element_insert_element_complex() {
    mu_start();
    //────────────────────
    BSTNode* root = NULL;
    Element element = { .type = ELEMENT_INT, .as_int = 123 };
    insert_element(&root, _copy_string("key"), element);
    element = { .type = ELEMENT_BOOLEAN, .as_boolean = true };
    insert_element(&root, _copy_string("second"), element);
    element = { .type = ELEMENT_NULL, .as_null = NULL };
    insert_element(&root, _copy_string("something"), element);

    // tree structure
    mu_check(root != NULL);
    mu_check(root->left == NULL);
    mu_check(root->right != NULL);
    mu_check(root->right->left == NULL);
    mu_check(root->right->right != NULL);
    mu_check(root->right->right->left == NULL);
    mu_check(root->right->right->right == NULL);
    // first tree element
    const Element* a_element = get_element(root, "key");
    mu_check(a_element != NULL);
    mu_check(a_element.type == ELEMENT_INT);
    mu_check(a_element.as_int == 123);
    // second element
    a_element = get_element(root, "second");
    mu_check(a_element != NULL);
    mu_check(a_element.type == ELEMENT_BOOLEAN);
    mu_check(a_element.as_boolean == true);
    // third element
    a_element = get_element(root, "something");
    mu_check(a_element != NULL);
    mu_check(a_element.type == ELEMENT_NULL);
    mu_check(a_element.as_null == NULL);

    a_element = get_element(root, "does not exist");
    mu_check(a_element == NULL);

    free_element((Element) {
        .type = ELEMENT_OBJECT,
        .as_object = root
    });
    //────────────────────
    mu_end();
}

int _test_empty_object() {
    mu_start();
    //────────────────────
    BSTNode* root;
    const char* input = "{}";
    const char* pos = input;
    bool is_success = parse_object(&root, &pos);
    mu_check(is_success);
    mu_check(root == NULL);
    mu_check(pos == input + 2);
    free_element((Element) {
        .type = ELEMENT_OBJECT,
        .as_object = root
    });
    //────────────────────
    mu_end();
}

int _test_object_single_pair() {
    mu_start();
    //────────────────────
    BSTNode* root;
    const char* input = "{\"key\": 123}abc";
    const char* pos = input;
    bool is_success = parse_object(&root, &pos);
    mu_check(is_success);
    mu_check(root != NULL);
    mu_check(root->left == NULL);
    mu_check(root->right == NULL);
    mu_check_strings_equal(root->key, "key");
    mu_check(root->element.type == ELEMENT_INT);
    mu_check(root->element.as_int == 123);
    mu_check(pos == input + strlen(input) - 3);
    free_element((Element) {
        .type = ELEMENT_OBJECT,
        .as_object = root
    });
    //────────────────────
    mu_end();
}

int _test_object_two_pairs() {
    mu_start();
    //────────────────────
    BSTNode* root;
    const char* input = "{\"key\": 123, \"second\": true} abc";
    const char* pos = input;
    bool is_success = parse_object(&root, &pos);
    mu_check(is_success);
    mu_check(pos == input + strlen(input) - 4);

    // tree structure
    mu_check(root != NULL);
    mu_check(root->left == NULL);
    mu_check(root->right != NULL);
    mu_check(root->right->left == NULL);
    mu_check(root->right->right == NULL);
    // first tree element
    mu_check_strings_equal(root->key, "key");
    mu_check(root->element.type == ELEMENT_INT);
    mu_check(root->element.as_int == 123);
    // second element
    mu_check_strings_equal(root->right->key, "second");
    mu_check(root->right->element.type == ELEMENT_BOOLEAN);
    mu_check(root->right->element.as_boolean == true);
    // free the tree
    free_element((Element) {
        .type = ELEMENT_OBJECT,
        .as_object = root
    });
    //────────────────────
    mu_end();
}

int _test_object_three_pairs() {
    mu_start();
    //────────────────────
    BSTNode* root;
    const char* input = "{\"key\": 123, \"second\": true, \"something\": null} abc";
    const char* pos = input;
    bool is_success = parse_object(&root, &pos);
    mu_check(is_success);
    mu_check(pos == input + strlen(input) - 4);

    // tree structure
    mu_check(root != NULL);
    mu_check(root->left == NULL);
    mu_check(root->right != NULL);
    mu_check(root->right->left == NULL);
    mu_check(root->right->right != NULL);
    mu_check(root->right->right->left == NULL);
    mu_check(root->right->right->right == NULL);
    // first tree element
    const Element* a_element = get_element(root, "key");
    mu_check(a_element != NULL);
    mu_check(a_element.type == ELEMENT_INT);
    mu_check(a_element.as_int == 123);
    // second element
    a_element = get_element(root, "second");
    mu_check(a_element != NULL);
    mu_check(a_element.type == ELEMENT_BOOLEAN);
    mu_check(a_element.as_boolean == true);
    // third element
    a_element = get_element(root, "something");
    mu_check(a_element != NULL);
    mu_check(a_element.type == ELEMENT_NULL);
    mu_check(a_element.as_null == NULL);
    // free the tree
    free_element((Element) {
        .type = ELEMENT_OBJECT,
        .as_object = root
    });
    //────────────────────
    mu_end();
}


int main(int argc, char* argv[]) {
    
    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2024 Alexander J. Quinn & David Burnett         This content is protected and may not be shared, uploaded, or distributed.