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 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include "miniunit.h"
#include "json.h"
// you are free to copy/adapt these examples
char* _copy_string(char const* original) {
char* copy = malloc(sizeof(*copy);
// TODO: implement copying
return copy;
}
static void _test_print_element_int() {
//────────────────────
Element element = { .type = ELEMENT_INT, .as_int = 5 };
print_element(element);
// expected: 5
free_element(element);
// expected: destory_element has no effect
//────────────────────
}
static void _test_print_element_string() {
//────────────────────
Element element = {
.type = ELEMENT_STRING,
.as_string = _copy_string("Hello") // string must be malloced
// otherwise cannot free
};
print_element(element);
// expected: "Hello"
free_element(element);
// expected: free_element should free the string
//────────────────────
}
static void _test_print_element_list() {
//────────────────────
// [ 1, 2, "three" ]
Node* head = NULL;
Node* new_node = malloc(sizeof(*head));
*new_node = (Node) {
.next = NULL,
.element.type = ELEMENT_INT,
.element.as_int = 1
};
head = new_node;
new_node = malloc(sizeof(*head));
*new_node = (Node) {
.next = NULL,
.element = (Element) { .type = ELEMENT_INT, .as_int = 2 }
};
head->next = new_node;
new_node = malloc(sizeof(*head));
*new_node = (Node) {
.next = NULL,
.element = (Element) {
.type = ELEMENT_STRING,
.as_string = _copy_string("three")
}
};
head->next->next = new_node;
Element list_element = { .type = ELEMENT_LIST, .as_list = head };
print_element(list_element);
// expected: [1,2,"three"]
free_element(list_element);
// let free_element handle freeing the list
// expected: free the three nodes,
// "destory" the contained elements on each
// ^
// destory_element
//────────────────────
}
static int _test_parse_list_123() {
mu_start();
//────────────────────
Node* head;
char const* input = "[1, 2, 3], 123";
char const* pos = input;
bool is_success = parse_list(&head, &pos);
mu_check(is_success);
// validate pos is at the right character
mu_check(pos == input + 9);
mu_check(*pos == ',');
// is head the right value?
mu_check(head != NULL);
mu_check(head->element.type == ELEMENT_INT);
mu_check(head->element.as_int == 1);
mu_check(head->next != NULL);
mu_check(head->next->element.type == ELEMENT_INT);
mu_check(head->next->element.as_int == 2);
mu_check(head->next->next != NULL);
mu_check(head->next->next->element.type == ELEMENT_INT);
mu_check(head->next->next->element.as_int == 3);
mu_check(head->next->next->next == NULL);
// calling free element so I do not need to recreate list freeing
// note: { is highlighted red, this is a vim bug, it is valid syntax
// I could also break this up over two lines
free_element((Element) {.type = ELEMENT_LIST, .as_list = head});
//────────────────────
mu_end();
}
int main(int argc, char* argv[]) {
_test_print_element_int();
_test_print_element_string();
_test_print_element_list();
mu_run(_test_parse_list_123);
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2022 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.