dte test coverage


Directory: ./
File: test/encoding.c
Date: 2024-12-21 16:03:22
Exec Total Coverage
Lines: 32 32 100.0%
Functions: 4 4 100.0%
Branches: 4 4 100.0%

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