Collect all of the various spread out 3rd party licenses to a single file.
[SquirrelJME.git] / nanocoat / tests / testStringDecodeChar.c
blob47dfb2ec2e9423c1a029373f39e7eced465261a3
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 #include "proto.h"
11 #include "sjme/util.h"
12 #include "test.h"
13 #include "unit.h"
15 /** The max string length. */
16 #define MAX_LEN 32
18 /** The number of strings to test. */
19 #define NUM_STRINGS 10
21 /** The input test strings. */
22 typedef struct testString
24 /** The input string to decode. */
25 sjme_jbyte in[MAX_LEN];
27 /** The resultant output. */
28 sjme_jint out[MAX_LEN];
29 } testString;
31 /** The actual test strings. */
32 static const testString testStrings[NUM_STRINGS] =
35 {0},
36 {-1},
40 {0x53, 0x71, 0x75, 0x65, 0x61, 0x6b, 0},
41 {0x53, 0x71, 0x75, 0x65, 0x61, 0x6b, -1}
45 {0x61, 0xc0, 0x80, 0x62, 0},
46 {0x61, 0x00, 0x62, -1}
50 {0x43, 0x7a, 0x65, 0xc5, 0x9b, 0xc4, 0x87,
51 0},
52 {0x43, 0x7a, 0x65, 0x15b, 0x107, -1}
56 {0xe4, 0xbd, 0xa0, 0xe5, 0xa5, 0xbd, 0},
57 {0x4f60, 0x597d, -1}
60 /* Invalid sequence. */
62 {0x61, 0x80, 0x62, 0},
63 {0x61, -1}
66 /* Invalid sequence. */
68 {0x61, 0xc0, 0x62, 0},
69 {0x61, -1}
72 /* Invalid sequence. */
74 {0xbd, 0xa0, 0xe5, 0xa5, 0xbd, 0},
75 {-1}
78 /* Invalid sequence. */
80 {0xe4, 0xa0, 0xe5, 0xa5, 0xbd, 0},
81 {-1}
84 /* Invalid sequence. */
86 {0xe4, 0xbd, 0xe5, 0xa5, 0xbd, 0},
87 {-1}
91 /**
92 * Tests the decoding of characters within modified-UTF style strings, using
93 * the function @c sjme_string_decodeChar .
95 * @since 2023/12/25
97 SJME_TEST_DECLARE(testStringDecodeChar)
99 const testString* testing;
100 sjme_jint i, charIndex;
101 sjme_jint c;
102 sjme_lpcstr string;
103 sjme_lpcstr p;
105 /* Test all the strings. */
106 for (i = 0; i < NUM_STRINGS; i++)
108 /* Which string are we testing? */
109 testing = &testStrings[i];
110 string = (sjme_lpcstr)testing->in;
112 /* Decode every character and check the input matches. */
113 charIndex = 0;
114 for (p = string; *p != 0; charIndex++)
116 /* These should never happen. */
117 if (charIndex >= MAX_LEN ||
118 ((uintptr_t)p - (uintptr_t)string) >= MAX_LEN)
119 sjme_unit_fail(test, "Reading input/output too far in?");
121 /* Decode. */
122 c = sjme_string_decodeChar(p, &p);
124 /* Must match the character index. */
125 sjme_unit_equalI(test, c, testing->out[charIndex],
126 "For input %d with output #%d, invalid result?",
127 i, charIndex);
129 /* End sequence? */
130 if (c == -1)
131 break;
135 /* Success! */
136 return SJME_TEST_RESULT_PASS;