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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

char* _copy_string(char const* original) {
    // TODO: implement
}

int _test_print_free_element_integer() {
    mu_start();
    //────────────────────
    Element element = {
        .type = ELEMENT_INT,
        .as_int = 8
    };
    print_element(element);
    free_element(element);
    //────────────────────
    mu_end();
}

int _test_print_free_element_string() {
    mu_start();
    //────────────────────
    Element element = {
        .type = ELEMENT_STRING,
        .as_string = _copy_string("Hello")
        // _copy_string is a helper like we wrote on HW09
    };
    print_element(element);
    // assuming print_element includes a newline
    free_element(element);
    //────────────────────
    mu_end();
}

// defined in json.c as an internal helper function
void _append_list_node(Node** a_head, Element element);

int _test_print_free_element_list_size_2() {
    mu_start();

    //────────────────────
    Node* head = malloc(sizeof(*head));
    *head = (Node) {
        .next = NULL,
        .element.type = ELEMENT_INT,
        .element.as_int = 9
    };
    Node* new_node = malloc(sizeof(*head));
    *new_node = (Node) {
        .next = NULL,
        .element.type = ELEMENT_STRING,
        .element.as_string = _copy_string("Hello 2")
    };
    head->next = new_node;

    Element element = {
        .type = ELEMENT_LIST,
        .as_list = head
    };
    print_element(element);
    free_element(element);
    //────────────────────
    mu_end();
}

int _test_parse_list_valid_size_3_only_integers_no_whitespace() {
    mu_start();
    //────────────────────
    Node* head;
    char const* input = "[1,2,3]";
    char const* pos = input;
    bool is_success = parse_list(&head, &pos);
    mu_check(is_success);
    mu_check(pos == input + 7);
    // test the list size
    mu_check(head != NULL); // list is size 1 or more
    mu_check(head->next != NULL); // list is size 2 or more
    mu_check(head->next->next != NULL); // list is size 3 or more
    mu_check(head->next->next->next == NULL); // list is size 3
    // test each element in the list
    Element element = head->element;
    mu_check(element.type == ELEMENT_INT);
    mu_check(element.as_int == 1);
    element = head->next->element;
    mu_check(element.type == ELEMENT_INT);
    mu_check(element.as_int == 2);
    element = head->next->next->element;
    mu_check(element.type == ELEMENT_INT);
    mu_check(element.as_int == 3);
    // free up the list
    // compound literal syntax
    /*
    free_element((Element){.type = ELEMENT_LIST, .as_list = head});
    */
    element = (Element){.type = ELEMENT_LIST, .as_list = head};
    free_element(element);
    //────────────────────
    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.