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 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include "miniunit.h"
#include "json.h"
// okay to copy/adapt this file
// copies a string into a heap allocated string
char* _copy_string(char const* original) {
char* copy = malloc(sizeof(*original) * (strlen(original) + 1));
strcpy(copy, original);
return copy;
}
void _test_print_element_int() {
Element element = {
.type = ELEMENT_INT,
.as_int = 7
};
print_element(element);
destory_element(element);
// prints
// 7
}
void _test_print_element_string() {
Element element = {
.type = ELEMENT_STRING,
.as_string = _copy_string("Hello")
};
print_element(element); // print element adds the ""
destory_element(element);
// prints
// "Hello"
}
void _test_print_element_list_size_3() {
Node* head = malloc(sizeof(*head));
*head = (Node) {
.next = NULL,
.element.type = ELEMENT_INT,
.element.as_int = 5
};
Node* new_node = malloc(sizeof(*new_node));
*new_node = (Node) {
.next = NULL;
.element.type = ELEMENT_INT,
.element.as_int = 7
};
head->next = new_node;
new_node = malloc(sizeof(*new_node));
*new_node = (Node) {
.next = NULL;
.element.type = ELEMENT_INT,
.element.as_int = 1
};
head->next->next = new_node;
Element element = {
.type = ELEMENT_LIST,
.as_list = head
};
print_element(element); // print element adds the ""
destory_element(element);
// prints
// [5, 7, 1]
}
static int _test_parse_list_size_2() {
mu_start();
//────────────────────
Node* head;
char const* input = "[1, 2]";
char const* pos = input;
bool is_success = parse_list(&head, &input);
mu_check(is_success);
// check structure of list
mu_check(head != NULL);
mu_check(head->next != NULL);
mu_check(head->next->next == NULL);
// check values in each node
mu_check(head->element.type == ELEMENT_INT);
mu_check(head->element.as_int == 1);
mu_check(head->next->element.type == ELEMENT_INT);
mu_check(head->next->element.as_int == 2);
// parenthesis highlighted red is actually a vim bug
// they never updated for compound initializers
free_element((Element){
.type == ELEMENT_LIST,
.as_list = head
});
/*
* this works the same as above
Element element = {
.type == ELEMENT_LIST,
.as_list = head
};
free_element(element);
*/
//────────────────────
mu_end();
}
int main(int argc, char* argv[]) {
_test_print_element_int();
_test_print_element_string();
_test_print_element_list_size_3();
mu_run(_test_parse_list_size_2);
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2023 Alexander J. Quinn, David J. Burnett This content is protected and may not be shared, uploaded, or distributed.