|
| 1 | +package org.stellar.sdk |
| 2 | + |
| 3 | +import io.kotest.assertions.throwables.shouldThrow |
| 4 | +import io.kotest.core.spec.style.FunSpec |
| 5 | +import io.kotest.datatest.withData |
| 6 | +import io.kotest.matchers.shouldBe |
| 7 | + |
| 8 | +class TOIDTest : |
| 9 | + FunSpec({ |
| 10 | + val ledgerFirst = 1L shl 32 |
| 11 | + val txFirst = 1L shl 12 |
| 12 | + val opFirst = 1L |
| 13 | + |
| 14 | + context("test toInt64 and fromInt64") { |
| 15 | + withData( |
| 16 | + nameFn = { (toid, id) -> |
| 17 | + "TOID(${toid.ledgerSequence}, ${toid.transactionOrder}, ${toid.operationIndex}) should be $id" |
| 18 | + }, |
| 19 | + TOID(0, 0, 1) to 1L, |
| 20 | + TOID(0, 0, 4095) to 4095L, |
| 21 | + TOID(0, 1, 0) to 4096L, |
| 22 | + TOID(0, 1048575, 0) to 4294963200L, |
| 23 | + TOID(1, 0, 0) to 4294967296L, |
| 24 | + TOID(2147483647, 0, 0) to 9223372032559808512L, |
| 25 | + TOID(1, 1, 1) to (ledgerFirst + txFirst + opFirst), |
| 26 | + TOID(1, 1, 0) to (ledgerFirst + txFirst), |
| 27 | + TOID(1, 0, 1) to (ledgerFirst + opFirst), |
| 28 | + TOID(1, 0, 0) to ledgerFirst, |
| 29 | + TOID(0, 1, 0) to txFirst, |
| 30 | + TOID(0, 0, 1) to opFirst, |
| 31 | + TOID(0, 0, 0) to 0L, |
| 32 | + TOID(2147483647, 1048575, 4095) to 9223372036854775807L, |
| 33 | + ) { (toid, id) -> |
| 34 | + toid.toInt64() shouldBe id |
| 35 | + TOID.fromInt64(id) shouldBe toid |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + context("test ledgerRangeInclusive") { |
| 40 | + test("ledger 1 to 1") { |
| 41 | + TOID.ledgerRangeInclusive(1, 1) shouldBe |
| 42 | + TOID.TOIDRange(TOID(0, 0, 0).toInt64(), TOID(2, 0, 0).toInt64()) |
| 43 | + } |
| 44 | + test("ledger 1 to 2") { |
| 45 | + TOID.ledgerRangeInclusive(1, 2) shouldBe |
| 46 | + TOID.TOIDRange(TOID(0, 0, 0).toInt64(), TOID(3, 0, 0).toInt64()) |
| 47 | + } |
| 48 | + test("ledger 2 to 2") { |
| 49 | + TOID.ledgerRangeInclusive(2, 2) shouldBe |
| 50 | + TOID.TOIDRange(TOID(2, 0, 0).toInt64(), TOID(3, 0, 0).toInt64()) |
| 51 | + } |
| 52 | + test("ledger 2 to 3") { |
| 53 | + TOID.ledgerRangeInclusive(2, 3) shouldBe |
| 54 | + TOID.TOIDRange(TOID(2, 0, 0).toInt64(), TOID(4, 0, 0).toInt64()) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + context("test incrementOperationIndex") { |
| 59 | + test("increment from 0") { |
| 60 | + val toid = TOID(0, 0, 0) |
| 61 | + toid.incrementOperationIndex() |
| 62 | + toid shouldBe TOID(0, 0, 1) |
| 63 | + toid.incrementOperationIndex() |
| 64 | + toid shouldBe TOID(0, 0, 2) |
| 65 | + toid.incrementOperationIndex() |
| 66 | + toid shouldBe TOID(0, 0, 3) |
| 67 | + } |
| 68 | + |
| 69 | + test("increment from max operation index") { |
| 70 | + val toid = TOID(0, 0, 0xFFF) |
| 71 | + toid.incrementOperationIndex() |
| 72 | + toid shouldBe TOID(1, 0, 0) |
| 73 | + toid.incrementOperationIndex() |
| 74 | + toid shouldBe TOID(1, 0, 1) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + context("test afterLedger") { |
| 79 | + test("after ledger 0") { TOID.afterLedger(0).toInt64() shouldBe 4294967295L } |
| 80 | + test("after ledger 1") { TOID.afterLedger(1).toInt64() shouldBe 8589934591L } |
| 81 | + test("after ledger 100") { TOID.afterLedger(100).toInt64() shouldBe 433791696895L } |
| 82 | + } |
| 83 | + |
| 84 | + context("test invalid parameters") { |
| 85 | + test("constructor with invalid params throws") { |
| 86 | + shouldThrow<IllegalArgumentException> { TOID(-1, 0, 0) } |
| 87 | + shouldThrow<IllegalArgumentException> { TOID(0, -1, 0) } |
| 88 | + shouldThrow<IllegalArgumentException> { TOID(0, 0, -1) } |
| 89 | + shouldThrow<IllegalArgumentException> { TOID(0, 0xFFFFF + 1, 0) } |
| 90 | + shouldThrow<IllegalArgumentException> { TOID(0, 0, 0xFFF + 1) } |
| 91 | + } |
| 92 | + |
| 93 | + test("fromInt64 with invalid params throws") { |
| 94 | + shouldThrow<IllegalArgumentException> { TOID.fromInt64(-1) } |
| 95 | + } |
| 96 | + |
| 97 | + test("ledgerRangeInclusive with invalid params throws") { |
| 98 | + shouldThrow<IllegalArgumentException> { TOID.ledgerRangeInclusive(2, 1) } |
| 99 | + shouldThrow<IllegalArgumentException> { TOID.ledgerRangeInclusive(0, 1) } |
| 100 | + shouldThrow<IllegalArgumentException> { TOID.ledgerRangeInclusive(-1, 100) } |
| 101 | + } |
| 102 | + } |
| 103 | + }) |
0 commit comments