Skip to content

Instantly share code, notes, and snippets.

@DragoonAethis
Last active October 15, 2017 20:29
Show Gist options
  • Save DragoonAethis/0ae0bbd7e7554f47e76090d09ad18a63 to your computer and use it in GitHub Desktop.
Save DragoonAethis/0ae0bbd7e7554f47e76090d09ad18a63 to your computer and use it in GitHub Desktop.
A lightweight tester to check if your UTF-8 -> UTF-16 conversion function works correctly. Further test cases can be added in the test_battery().
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
// How many tests passed?
int tests = 0;
int passed = 0;
struct test_case {
int id;
unsigned char utf8_input[5];
unsigned char utf16_test[5];
};
void test_battery();
void run_test(struct test_case tcase);
unsigned char * utf8_to_utf16(unsigned char * utf8);
// Run a single test case - call the converter, compare
// the output against the expected utf16_test.
void run_test(struct test_case tcase) {
printf("Test %d... ", tcase.id);
tests++;
unsigned char * output = utf8_to_utf16(tcase.utf8_input);
size_t test_length = strlen(tcase.utf16_test);
size_t output_length = strlen(output);
if (test_length != output_length) {
printf("\033[1;31mFAILED!\033[0m\n");
printf("Expected %d bytes, got %d bytes.\n", strlen(tcase.utf16_test), strlen(output));
free(output);
return;
} else if (memcmp(tcase.utf16_test, output, output_length) != 0) {
printf("\033[1;31mFAILED!\033[0m\n");
printf("Output mismatch, got: ");
for (int i = 0; i < output_length; i++) {
printf("0x%02X ", (unsigned char)output[i]);
}
printf("\n");
free(output);
return;
}
passed++;
free(output);
printf("\033[1;32mOK!\033[0m\n");
}
int main() {
test_battery();
printf("Passed %d/%d tests. ", passed, tests);
if (passed == tests) {
printf("See you next mission!\n");
} else {
printf("Good luck next time!\n");
}
}
// --- Interesting stuff below... ---
void test_battery() {
run_test((struct test_case){0, { 0x30, 0 }, { 0x30, 0x00, 0 }}); // 1-byte ASCII
run_test((struct test_case){1, { 0x31, 0 }, { 0x31, 0x00, 0 }});
run_test((struct test_case){2, { 0x32, 0 }, { 0x32, 0x00, 0 }});
run_test((struct test_case){3, { 0xCE, 0xA9, 0 }, { 0xA9, 0x03, 0 }}); // 2-byte UTF-8
run_test((struct test_case){4, { 0xCA, 0x98, 0 }, { 0x98, 0x02, 0 }});
run_test((struct test_case){5, { 0xD6, 0x8D, 0 }, { 0x8D, 0x05, 0 }});
run_test((struct test_case){6, { 0xD1, 0xAC, 0 }, { 0x6C, 0x04, 0 }});
run_test((struct test_case){7, { 0xE0, 0xAE, 0x94, 0 }, { 0x94, 0x0B, 0 }}); // 3-byte UTF-8
run_test((struct test_case){8, { 0xE0, 0xAE, 0x87, 0 }, { 0x87, 0x0B, 0 }});
run_test((struct test_case){9, { 0xE1, 0xAE, 0xBF, 0 }, { 0xBF, 0x1B, 0 }});
run_test((struct test_case){10, { 0xE1, 0xAF, 0xBC, 0 }, { 0xFC, 0x1B, 0 }});
run_test((struct test_case){11, { 0xE3, 0xBA, 0xBA, 0 }, { 0xBA, 0x3E, 0 }});
run_test((struct test_case){12, { 0xF0, 0x90, 0x90, 0xB2, 0 }, { 0x41, 0xD8, 0x32, 0xDC, 0 }}); // Have fun!
run_test((struct test_case){1337, { 0xF0, 0x92, 0x88, 0xA2, 0 }, { 0x48, 0xD8, 0x22, 0xDE, 0 }});
}
// *********************************
// * PASTE YOUR utf8_to_utf16 HERE *
// *********************************
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment