From 6d471d213717482903e69d9b95a2af9d671a495e Mon Sep 17 00:00:00 2001 From: Dimitri Lozeve Date: Thu, 4 Jun 2020 12:43:12 +0200 Subject: [PATCH] Add test for frequency_score --- tests/main.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/main.c b/tests/main.c index dcdc364..690a0c4 100644 --- a/tests/main.c +++ b/tests/main.c @@ -1,6 +1,7 @@ #define MUNIT_ENABLE_ASSERT_ALIASES #include "munit.h" #include "utils.h" +#include #include static MunitResult test_hex_to_byte(const MunitParameter params[], void *data) { @@ -36,11 +37,35 @@ static MunitResult test_hex_to_base64(const MunitParameter params[], return MUNIT_OK; } +static MunitResult test_frequency_score(const MunitParameter params[], + void *data) { + (void)params; + (void)data; + + // Non-printable characters should return INFINITY. + assert(isfinite(frequency_score("YELLOW SUBMARINE", 16))); + assert(isinf(frequency_score("\0x07", 1))); + assert(isinf(frequency_score("YELLOW\0x07 SUBMARINE", 17))); + + // Teh frequency score should be case-insensitive. + assert_double_equal(frequency_score("YELLOW SUBMARINE", 16), + frequency_score("yellow submarine", 16), 12); + + // E is more frequent than Z. + assert_double(frequency_score("eee", 3), <, frequency_score("zzz", 3)); + // Space is more frequent than anything. + assert_double(frequency_score(" ", 3), <, frequency_score("eee", 3)); + + return MUNIT_OK; +} + static MunitTest test_suite_tests[] = { {"/hex_to_byte", test_hex_to_byte, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}, {"/hex_to_base64", test_hex_to_base64, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}, + {"/frequency_score", test_frequency_score, NULL, NULL, + MUNIT_TEST_OPTION_NONE, NULL}, {NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}}; static const MunitSuite test_suite = {(char *)"", test_suite_tests, NULL, 1,