14
14
* limitations under the License.
15
15
*/
16
16
17
- package com.google.firebase.dataconnect
17
+ package com.google.firebase.dataconnect.util
18
18
19
19
import com.google.firebase.dataconnect.util.AlphanumericStringUtil.toAlphaNumericString
20
20
import io.kotest.matchers.shouldBe
21
+ import io.kotest.property.Arb
22
+ import io.kotest.property.arbitrary.byte
23
+ import io.kotest.property.arbitrary.byteArray
24
+ import io.kotest.property.arbitrary.int
25
+ import io.kotest.property.checkAll
26
+ import kotlinx.coroutines.test.runTest
21
27
import org.junit.Test
22
28
23
- class UtilUnitTest {
29
+ class AlphanumericStringUtilUnitTest {
24
30
25
31
@Test
26
- fun `ByteArray toAlphaNumericString() interprets the alphabet` () {
32
+ fun `toAlphaNumericString() interprets the alphabet` () {
27
33
val byteArray =
28
34
byteArrayOf(
29
35
0 ,
@@ -52,13 +58,13 @@ class UtilUnitTest {
52
58
}
53
59
54
60
@Test
55
- fun `ByteArray toAlphaNumericString() where the final 5-bit chunk is 1 bit` () {
61
+ fun `toAlphaNumericString() where the final 5-bit chunk is 1 bit` () {
56
62
byteArrayOf(75 , 50 ).let { it.toAlphaNumericString() shouldBe " bet2" }
57
63
byteArrayOf(75 , 51 ).let { it.toAlphaNumericString() shouldBe " bet3" }
58
64
}
59
65
60
66
@Test
61
- fun `ByteArray toAlphaNumericString() where the final 5-bit chunk is 2 bits` () {
67
+ fun `toAlphaNumericString() where the final 5-bit chunk is 2 bits` () {
62
68
byteArrayOf(117 , - 40 , - 116 , - 66 , - 105 , - 61 , 18 , - 117 , - 52 ).let {
63
69
it.toAlphaNumericString() shouldBe " greathorsebarn2"
64
70
}
@@ -68,61 +74,61 @@ class UtilUnitTest {
68
74
}
69
75
70
76
@Test
71
- fun `ByteArray toAlphaNumericString() where the final 5-bit chunk is 3 bits` () {
77
+ fun `toAlphaNumericString() where the final 5-bit chunk is 3 bits` () {
72
78
byteArrayOf(64 ).let { it.toAlphaNumericString() shouldBe " a2" }
73
79
byteArrayOf(71 ).let { it.toAlphaNumericString() shouldBe " a9" }
74
80
}
75
81
76
82
@Test
77
- fun `ByteArray toAlphaNumericString() where the final 5-bit chunk is 4 bits` () {
83
+ fun `toAlphaNumericString() where the final 5-bit chunk is 4 bits` () {
78
84
byteArrayOf(- 58 , 117 , 48 ).let { it.toAlphaNumericString() shouldBe " stun2" }
79
85
byteArrayOf(- 58 , 117 , 63 ).let { it.toAlphaNumericString() shouldBe " stunh" }
80
86
}
81
87
82
88
@Test
83
- fun `ByteArray toAlphaNumericString() on empty byte array` () {
89
+ fun `toAlphaNumericString() on empty byte array` () {
84
90
val emptyByteArray = byteArrayOf()
85
91
emptyByteArray.toAlphaNumericString() shouldBe " "
86
92
}
87
93
88
94
@Test
89
- fun `ByteArray toAlphaNumericString() on byte array with 1 element of value 0` () {
95
+ fun `toAlphaNumericString() on byte array with 1 element of value 0` () {
90
96
val byteArray = byteArrayOf(0 )
91
97
byteArray.toAlphaNumericString() shouldBe " 22"
92
98
}
93
99
94
100
@Test
95
- fun `ByteArray toAlphaNumericString() on byte array with 1 element of value 1` () {
101
+ fun `toAlphaNumericString() on byte array with 1 element of value 1` () {
96
102
val byteArray = byteArrayOf(1 )
97
103
byteArray.toAlphaNumericString() shouldBe " 23"
98
104
}
99
105
100
106
@Test
101
- fun `ByteArray toAlphaNumericString() on byte array with 1 element of value 0xff` () {
107
+ fun `toAlphaNumericString() on byte array with 1 element of value 0xff` () {
102
108
val byteArray = byteArrayOf(0xff .toByte())
103
109
byteArray.toAlphaNumericString() shouldBe " z9"
104
110
}
105
111
106
112
@Test
107
- fun `ByteArray toAlphaNumericString() on byte array with 1 element of value -1` () {
113
+ fun `toAlphaNumericString() on byte array with 1 element of value -1` () {
108
114
val byteArray = byteArrayOf(- 1 )
109
115
byteArray.toAlphaNumericString() shouldBe " z9"
110
116
}
111
117
112
118
@Test
113
- fun `ByteArray toAlphaNumericString() on byte array with 1 element of value MIN_VALUE` () {
119
+ fun `toAlphaNumericString() on byte array with 1 element of value MIN_VALUE` () {
114
120
val byteArray = byteArrayOf(Byte .MIN_VALUE )
115
121
byteArray.toAlphaNumericString() shouldBe " j2"
116
122
}
117
123
118
124
@Test
119
- fun `ByteArray toAlphaNumericString() on byte array with 1 element of value MAX_VALUE` () {
125
+ fun `toAlphaNumericString() on byte array with 1 element of value MAX_VALUE` () {
120
126
val byteArray = byteArrayOf(Byte .MAX_VALUE )
121
127
byteArray.toAlphaNumericString() shouldBe " h9"
122
128
}
123
129
124
130
@Test
125
- fun `ByteArray toAlphaNumericString() on byte array containing all possible values` () {
131
+ fun `toAlphaNumericString() on byte array containing all possible values` () {
126
132
val byteArray =
127
133
buildList {
128
134
for (i in 0 until 512 ) {
@@ -142,6 +148,12 @@ class UtilUnitTest {
142
148
" rv3efmqguddfprr4wvpgxwrqzdzj83sd3wbkg8sz6enmqdtn8wxnyju9bf9p8puvdxkqguvhgfvrqzw5jy7sz6" +
143
149
" wrnghu9bxdpytvhgxzsh5wrnynuzfxzsz9xhrz9xzvz3"
144
150
}
151
+
152
+ @Test
153
+ fun `toAlphaNumericString() with random byte arrays` () = runTest {
154
+ val byteArrayArb = Arb .byteArray(length = Arb .int(0 , 100 ), content = Arb .byte())
155
+ checkAll(100_000 , byteArrayArb) { byteArray -> byteArray.toAlphaNumericString() }
156
+ }
145
157
}
146
158
147
159
/*
0 commit comments