@@ -2,6 +2,7 @@ package de.cketti.codepoints.deluxe
2
2
3
3
import kotlin.test.assertEquals
4
4
import kotlin.test.Test
5
+ import kotlin.test.assertFailsWith
5
6
6
7
class CharSequenceExtensionsTest {
7
8
@Test
@@ -57,4 +58,131 @@ class CharSequenceExtensionsTest {
57
58
assertEquals(0xD83E .toCodePoint(), " \uD83E\uDD95\uD83E\uDD96 " .codePointBefore(1 ))
58
59
assertEquals(0xD83E .toCodePoint(), " \uD83E\uDD95\uD83E\uDD96 " .codePointBefore(3 ))
59
60
}
61
+
62
+ @Test
63
+ fun forEachCodepoint () {
64
+ fun CharSequence.collectCodepoints (): List <CodePoint > = buildList { forEachCodePoint { add(it) } }
65
+
66
+ assertEquals(
67
+ emptyList(),
68
+ " " .collectCodepoints(),
69
+ )
70
+ assertEquals(
71
+ listOf (' a' .toCodePoint()),
72
+ " a" .collectCodepoints(),
73
+ )
74
+ assertEquals(
75
+ listOf (' a' .toCodePoint(), 0xFFFF .toCodePoint()),
76
+ " a\uFFFF " .collectCodepoints(),
77
+ )
78
+ assertEquals(
79
+ listOf (0x1F995 .toCodePoint(), ' a' .toCodePoint(), 0x1F996 .toCodePoint()),
80
+ " \uD83E\uDD95 a\uD83E\uDD96 " .collectCodepoints(),
81
+ )
82
+ }
83
+
84
+ @Test
85
+ fun forEachCodepoint_with_non_default_indexes () {
86
+ fun CharSequence.collectCodepoints (
87
+ startIndex : Int ,
88
+ endIndex : Int ,
89
+ ): List <CodePoint > = buildList { forEachCodePoint(startIndex, endIndex) { add(it) } }
90
+
91
+ assertEquals(
92
+ listOf (' a' .toCodePoint()),
93
+ " ab" .collectCodepoints(0 , 1 ),
94
+ )
95
+ assertEquals(
96
+ listOf (' b' .toCodePoint()),
97
+ " ab" .collectCodepoints(1 , 2 ),
98
+ )
99
+ assertEquals(
100
+ listOf (' a' .toCodePoint()),
101
+ " \uD83E\uDD95 a\uD83E\uDD96 " .collectCodepoints(2 , 3 ),
102
+ )
103
+ assertEquals(
104
+ listOf (0xD83E .toCodePoint()),
105
+ " \uD83E\uDD95 a\uD83E\uDD96 " .collectCodepoints(0 , 1 ),
106
+ )
107
+ assertEquals(
108
+ listOf (0xDD95 .toCodePoint()),
109
+ " \uD83E\uDD95 a\uD83E\uDD96 " .collectCodepoints(1 , 2 ),
110
+ )
111
+ assertEquals(
112
+ listOf (0xDD95 .toCodePoint(), ' a' .toCodePoint(), 0xD83E .toCodePoint()),
113
+ " \uD83E\uDD95 a\uD83E\uDD96 " .collectCodepoints(1 , 4 ),
114
+ )
115
+ assertFailsWith(IllegalArgumentException ::class ) {
116
+ " a" .forEachCodePoint(startIndex = 1 , endIndex = 0 ) { }
117
+ }
118
+ assertFailsWith(IllegalArgumentException ::class ) {
119
+ " a" .forEachCodePoint(startIndex = 1 , endIndex = 2 ) { }
120
+ }
121
+ }
122
+
123
+ @Test
124
+ fun forEachCodepointIndexed () {
125
+ fun CharSequence.collectCodepoints (): List <Pair <Int , CodePoint >> =
126
+ buildList { forEachCodePointIndexed { index, codepoint -> add(index to codepoint) } }
127
+
128
+ assertEquals(
129
+ emptyList(),
130
+ " " .collectCodepoints(),
131
+ )
132
+ assertEquals(
133
+ listOf (0 to ' a' .toCodePoint()),
134
+ " a" .collectCodepoints(),
135
+ )
136
+ assertEquals(
137
+ listOf (0 to ' a' .toCodePoint(), 1 to 0x1F995 .toCodePoint()),
138
+ " a\uD83E\uDD95 " .collectCodepoints(),
139
+ )
140
+ assertEquals(
141
+ listOf (
142
+ 0 to 0x1F995 .toCodePoint(),
143
+ 2 to ' a' .toCodePoint(),
144
+ 3 to 0x1F996 .toCodePoint(),
145
+ ),
146
+ " \uD83E\uDD95 a\uD83E\uDD96 " .collectCodepoints(),
147
+ )
148
+ }
149
+
150
+ @Test
151
+ fun forEachCodepointIndexed_with_non_default_indexes () {
152
+ fun CharSequence.collectCodepoints (start : Int , end : Int ): List <Pair <Int , CodePoint >> =
153
+ buildList { forEachCodePointIndexed(start, end) { index, codepoint -> add(index to codepoint) } }
154
+
155
+ assertEquals(
156
+ listOf (0 to ' a' .toCodePoint()),
157
+ " ab" .collectCodepoints(0 , 1 ),
158
+ )
159
+ assertEquals(
160
+ listOf (1 to ' b' .toCodePoint()),
161
+ " ab" .collectCodepoints(1 , 2 ),
162
+ )
163
+ assertEquals(
164
+ listOf (1 to 0x1F995 .toCodePoint()),
165
+ " a\uD83E\uDD95 " .collectCodepoints(1 , 3 ),
166
+ )
167
+ assertEquals(
168
+ listOf (
169
+ 1 to 0xDD95 .toCodePoint(),
170
+ 2 to ' a' .toCodePoint(),
171
+ 3 to 0xD83E .toCodePoint(),
172
+ ),
173
+ " \uD83E\uDD95 a\uD83E\uDD96 " .collectCodepoints(1 , 4 ),
174
+ )
175
+ assertEquals(
176
+ listOf (
177
+ 2 to ' a' .toCodePoint(),
178
+ ),
179
+ " \uD83E\uDD95 a\uD83E\uDD96 " .collectCodepoints(2 , 3 ),
180
+ )
181
+ assertFailsWith(IllegalArgumentException ::class ) {
182
+ " a" .forEachCodePointIndexed(startIndex = 1 , endIndex = 0 ) { _, _ -> }
183
+ }
184
+ assertFailsWith(IllegalArgumentException ::class ) {
185
+ " a" .forEachCodePointIndexed(startIndex = 1 , endIndex = 2 ) { _, _ -> }
186
+ }
187
+ }
60
188
}
0 commit comments