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 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
// okay to copy/adapt
// one of these might be helpful for testing find result
#define mu_check_strings_equal_length(s1, s2, length)
// ---- OR ----
#define mu_check_find_result(expected, find_result)
char* _copy_string(char const* str) {
char* result_str = malloc(sizeof(*result_str) * (strlen(str) + 1));
strcpy(result_str, str);
return result_str;
}
int _test_free_strings_two_strings() {
mu_start();
//────────────────────
char** strings = malloc(sizeof(*strings) * 2);
strings[0] = _copy_string("Hello");
strings[1] = _copy_string("World");
struct Strings strings = { .strings = strings, .num_strings = 2 };
free_strings(&strings);
//────────────────────
mu_end();
}
int _test_copy_substring_length_five() {
mu_start();
//────────────────────
char const* input = "Hello World";
char* result = copy_substring(input, 5);
mu_check(result != NULL);
mu_check_strings_equal(result, "Hello");
free(result);
//────────────────────
mu_end();
}
int _test_find_substring_one_needle_one_occurance() {
mu_start();
//────────────────────
char const* input = "Hello World";
char const* pos = input;
char* strings[] = { " " };
struct Strings needles = { .strings = strings, .num_strings = 1 };
struct FindResult result = find_substring(&pos, needles);
mu_check_strings_equal(" ", result.needle);
mu_check(result.idx == 5);:w
mu_check(strncmp(" ", input + result.idx, 1) == 0);
mu_check(pos == input + 6);
//────────────────────
mu_end();
}
// you will want *many* more tests than this most likely
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.