-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathCellBuilderTest.kt
95 lines (85 loc) · 3.52 KB
/
CellBuilderTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package org.ton.cell
import org.ton.bigint.BigInt
import org.ton.bitstring.BitString
import org.ton.crypto.hex
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFails
class CellBuilderTest {
@Test
fun `build empty`() {
val cell = CellBuilder.beginCell()
.endCell()
assertEquals(0, cell.bits.size)
assertEquals(Cell(), cell)
}
@Test
fun `build single bit`() {
val cell = CellBuilder.beginCell()
.storeBit(true)
.endCell()
assertEquals(Cell(BitString.of(true)), cell)
}
@Test
fun `build multiple bits`() {
val cell = CellBuilder.beginCell()
.storeBit(true)
.storeBit(false)
.storeBit(false)
.storeBit(true)
.storeBit(false)
.endCell()
assertEquals(Cell(BitString.of(true, false, false, true, false)), cell)
}
@Test
fun `fail on too many bits added`() {
val builder = CellBuilder.beginCell(10)
.storeUInt(0, 10) // fine for now
assertEquals(10, builder.bits.size)
assertFails {
builder.storeBit(false)
}
}
@Test
fun `build a number`() {
assertEquals(Cell("00"), CellBuilder.createCell { storeUInt(0, 8) })
assertEquals(Cell("01"), CellBuilder.createCell { storeUInt(1, 8) })
assertEquals(Cell("05"), CellBuilder.createCell { storeUInt(5, 8) })
assertEquals(Cell("21"), CellBuilder.createCell { storeUInt(33, 8) })
assertEquals(Cell("7F"), CellBuilder.createCell { storeUInt(127, 8) })
assertEquals(Cell("80"), CellBuilder.createCell { storeUInt(128, 8) })
assertEquals(Cell("FF"), CellBuilder.createCell { storeUInt(255, 8) })
assertEquals(Cell("804_"), CellBuilder.createCell { storeUInt(256, 9) })
assertFails { CellBuilder.createCell { storeUInt(256, 8) } }
assertFails { CellBuilder.createCell { storeUInt(-1, 8) } }
assertEquals(Cell("7F"), CellBuilder.createCell { storeInt(127, 8) })
assertEquals(Cell("00"), CellBuilder.createCell { storeInt(0, 8) })
assertEquals(Cell("FF"), CellBuilder.createCell { storeInt(-1, 8) })
assertEquals(Cell("FB"), CellBuilder.createCell { storeInt(-5, 8) })
assertEquals(Cell("DF"), CellBuilder.createCell { storeInt(-33, 8) })
assertEquals(Cell("81"), CellBuilder.createCell { storeInt(-127, 8) })
assertEquals(Cell("80"), CellBuilder.createCell { storeInt(-128, 8) })
assertEquals(Cell("BFC_"), CellBuilder.createCell { storeInt(-129, 9) })
assertEquals(Cell("4357"), CellBuilder.createCell { storeInt(17239, 16) })
assertEquals(Cell("FDF_"), CellBuilder.createCell { storeInt(-17, 11) })
assertEquals(Cell("3B9ACAEF"), CellBuilder.createCell { storeInt(1000000239, 32) })
assertEquals(
Cell("00000001BC16E45E4D41643_"),
CellBuilder.createCell { storeInt(1000000239L * 1000000239, 91) }
)
assertEquals(
Cell("989A386C05EFF862FFFFE23_"),
CellBuilder.createCell { storeInt(BigInt("-1000000000000000000000000239"), 91) })
assertFails { CellBuilder.createCell { storeUInt(-129, 8) } }
}
@Test
fun `build multiple numbers`() {
val cell = CellBuilder.beginCell()
.storeUInt(0, 16)
.storeUInt(1, 8)
.storeUInt(1, 8)
.storeUInt(69, 64)
.endCell()
assertEquals("000001010000000000000045", hex(cell.bits.toByteArray()))
}
}