|
1 | 1 | //@ args = '--string-encoding utf16' |
2 | 2 | //@ [lang] |
3 | | -//@ cflags = '-Wno-c++-keyword' |
| 3 | +//@ cflags = '-Wno-c++-compat' |
4 | 4 |
|
5 | 5 | #include <assert.h> |
6 | 6 | #include <stdlib.h> |
@@ -40,4 +40,60 @@ void exports_runner_run() { |
40 | 40 | test_strings_to_test_roundtrip(&str4, &str5); |
41 | 41 | assert_str(&str5, u"🚀🚀🚀 𠈄𓀀"); |
42 | 42 | runner_string_free(&str5); |
| 43 | + |
| 44 | + // Basic substring extraction |
| 45 | + runner_string_t str6; |
| 46 | + const char16_t *source = u"hello world"; |
| 47 | + runner_string_dup_n(&str6, source, 5); |
| 48 | + assert(str6.len == 5); |
| 49 | + assert(memcmp(str6.ptr, u"hello", 5 * 2) == 0); |
| 50 | + runner_string_free(&str6); |
| 51 | + |
| 52 | + // Zero length (edge case - boundary condition) |
| 53 | + runner_string_t str7; |
| 54 | + runner_string_dup_n(&str7, u"test", 0); |
| 55 | + assert(str7.len == 0); |
| 56 | + runner_string_free(&str7); |
| 57 | + |
| 58 | + // Full string length |
| 59 | + runner_string_t str8; |
| 60 | + const char16_t *full_str = u"complete"; |
| 61 | + size_t full_len = 8; |
| 62 | + runner_string_dup_n(&str8, full_str, full_len); |
| 63 | + assert(str8.len == full_len); |
| 64 | + assert(memcmp(str8.ptr, full_str, full_len * 2) == 0); |
| 65 | + runner_string_free(&str8); |
| 66 | + |
| 67 | + // Substring from middle (pointer offset) |
| 68 | + runner_string_t str9; |
| 69 | + const char16_t *middle_source = u"prefix_target_suffix"; |
| 70 | + runner_string_dup_n(&str9, middle_source + 7, 6); |
| 71 | + assert(str9.len == 6); |
| 72 | + assert(memcmp(str9.ptr, u"target", 6 * 2) == 0); |
| 73 | + runner_string_free(&str9); |
| 74 | + |
| 75 | + // Unicode content with explicit length |
| 76 | + runner_string_t str10; |
| 77 | + const char16_t *unicode_src = u"🚀🚀🚀 test"; |
| 78 | + // Each rocket emoji is 2 UTF-16 code units (surrogate pair), space is 1, "test" is 4 |
| 79 | + // Total: 6 + 1 + 4 = 11 code units, extract first 7 (3 rockets + space) |
| 80 | + runner_string_dup_n(&str10, unicode_src, 7); |
| 81 | + assert(str10.len == 7); |
| 82 | + assert(memcmp(str10.ptr, u"🚀🚀🚀 ", 7 * 2) == 0); |
| 83 | + runner_string_free(&str10); |
| 84 | + |
| 85 | + // Single character |
| 86 | + runner_string_t str11; |
| 87 | + runner_string_dup_n(&str11, u"x", 1); |
| 88 | + assert(str11.len == 1); |
| 89 | + assert(str11.ptr[0] == u'x'); |
| 90 | + runner_string_free(&str11); |
| 91 | + |
| 92 | + // Verify data independence (modification doesn't affect original) |
| 93 | + runner_string_t str12; |
| 94 | + char16_t mutable_src[] = u"original"; |
| 95 | + runner_string_dup_n(&str12, mutable_src, 8); |
| 96 | + mutable_src[0] = u'X'; |
| 97 | + assert(str12.ptr[0] == u'o'); |
| 98 | + runner_string_free(&str12); |
43 | 99 | } |
0 commit comments