dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 100.0% 32 / 0 / 32
Functions: 100.0% 4 / 0 / 4
Branches: 100.0% 4 / 0 / 4

test/encoding.c
Line Branch Exec Source
1 #include "test.h"
2 #include "encoding.h"
3
4 1 static void test_detect_encoding_from_bom(TestContext *ctx)
5 {
6 1 static const struct bom_test {
7 EncodingType encoding;
8 StringView text;
9 } tests[] = {
10 {UTF8, STRING_VIEW("\xef\xbb\xbfHello")},
11 {UTF32BE, STRING_VIEW("\x00\x00\xfe\xffHello")},
12 {UTF32LE, STRING_VIEW("\xff\xfe\x00\x00Hello")},
13 {UTF16BE, STRING_VIEW("\xfe\xffHello")},
14 {UTF16LE, STRING_VIEW("\xff\xfeHello")},
15 {UTF16LE, STRING_VIEW("\xff\xfe")},
16 {UNKNOWN_ENCODING, STRING_VIEW("\xff\xff\x00\x00Hello")},
17 {UNKNOWN_ENCODING, STRING_VIEW("\x00\xef\xbb\xbfHello")},
18 {UNKNOWN_ENCODING, STRING_VIEW("\xef\xbb")},
19 {UNKNOWN_ENCODING, STRING_VIEW("\xef\xbb#")},
20 {UNKNOWN_ENCODING, STRING_VIEW("\xee\xbb\xbf")},
21 {UNKNOWN_ENCODING, STRING_VIEW("\xff\xbb\xbf")},
22 {UNKNOWN_ENCODING, STRING_VIEW("\xbf\xbb\xef")},
23 {UNKNOWN_ENCODING, STRING_VIEW("\x00\x00\xfe")},
24 {UNKNOWN_ENCODING, STRING_VIEW("\x00")},
25 {UNKNOWN_ENCODING, STRING_VIEW("")},
26 {UNKNOWN_ENCODING, STRING_VIEW_INIT},
27 };
28
2/2
✓ Branch 6 → 3 taken 17 times.
✓ Branch 6 → 7 taken 1 time.
18 FOR_EACH_I(i, tests) {
29 17 const struct bom_test *t = &tests[i];
30 17 EncodingType result = detect_encoding_from_bom(t->text);
31 17 IEXPECT_EQ(result, t->encoding);
32 }
33 1 }
34
35 1 static void test_lookup_encoding(TestContext *ctx)
36 {
37 1 static const struct {
38 EncodingType encoding;
39 const char *name;
40 } tests[] = {
41 {UTF8, "UTF-8"},
42 {UTF8, "UTF8"},
43 {UTF8, "utf-8"},
44 {UTF8, "utf8"},
45 {UTF8, "Utf8"},
46 {UTF16BE, "UTF16"},
47 {UTF16BE, "UTF-16"},
48 {UTF32BE, "utf32"},
49 {UTF32BE, "utf-32"},
50 {UTF32LE, "utf-32le"},
51 {UTF32LE, "ucs-4le"},
52 {UTF32BE, "ucs-4BE"},
53 {UTF32BE, "ucs-4"},
54 {UTF32BE, "ucs4"},
55 {UNKNOWN_ENCODING, "UTF8_"},
56 {UNKNOWN_ENCODING, "UTF"},
57 };
58
2/2
✓ Branch 6 → 3 taken 16 times.
✓ Branch 6 → 7 taken 1 time.
17 FOR_EACH_I(i, tests) {
59 16 EncodingType result = lookup_encoding(tests[i].name);
60 16 IEXPECT_EQ(result, tests[i].encoding);
61 }
62 1 }
63
64 1 static void test_encoding_from_type(TestContext *ctx)
65 {
66 1 const char *a = encoding_from_type(UTF8);
67 1 EXPECT_STREQ(a, "UTF-8");
68 1 EXPECT_TRUE(encoding_is_utf8(a));
69 // Ensure returned value is an "interned" string
70 1 EXPECT_PTREQ(a, encoding_normalize("utf8"));
71 1 EXPECT_PTREQ(a, encoding_from_type(UTF8));
72 1 }
73
74 1 static void test_get_bom_for_encoding(TestContext *ctx)
75 {
76 1 const ByteOrderMark *bom = get_bom_for_encoding(UTF8);
77 1 EXPECT_MEMEQ(bom->bytes, bom->len, "\xef\xbb\xbf", 3);
78
79 1 bom = get_bom_for_encoding(UTF32LE);
80 1 EXPECT_MEMEQ(bom->bytes, bom->len, "\xff\xfe\0\0", 4);
81
82 1 bom = get_bom_for_encoding(UTF16BE);
83 1 EXPECT_MEMEQ(bom->bytes, bom->len, "\xfe\xff", 2);
84
85 1 EXPECT_NULL(get_bom_for_encoding(UNKNOWN_ENCODING));
86 1 EXPECT_FALSE(encoding_type_has_bom(UNKNOWN_ENCODING));
87 1 EXPECT_TRUE(encoding_type_has_bom(UTF8));
88 1 EXPECT_TRUE(encoding_type_has_bom(UTF32LE));
89 1 }
90
91 static const TestEntry tests[] = {
92 TEST(test_detect_encoding_from_bom),
93 TEST(test_lookup_encoding),
94 TEST(test_encoding_from_type),
95 TEST(test_get_bom_for_encoding),
96 };
97
98 const TestGroup encoding_tests = TEST_GROUP(tests);
99