|
| 1 | +/* |
| 2 | + * Copyright 2019-2020 JetBrains s.r.o. |
| 3 | + * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. |
| 4 | + */ |
| 5 | +/* Based on the ThreeTenBp project. |
| 6 | + * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos |
| 7 | + */ |
| 8 | + |
| 9 | +package kotlinx.time.test |
| 10 | + |
| 11 | +import kotlinx.time.* |
| 12 | +import kotlin.test.* |
| 13 | + |
| 14 | +class ThreeTenBpInstantTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + fun instantComparisons() { |
| 18 | + val instants = arrayOf( |
| 19 | + Instant.fromEpochSeconds(-2L, 0), |
| 20 | + Instant.fromEpochSeconds(-2L, 999999998), |
| 21 | + Instant.fromEpochSeconds(-2L, 999999999), |
| 22 | + Instant.fromEpochSeconds(-1L, 0), |
| 23 | + Instant.fromEpochSeconds(-1L, 1), |
| 24 | + Instant.fromEpochSeconds(-1L, 999999998), |
| 25 | + Instant.fromEpochSeconds(-1L, 999999999), |
| 26 | + Instant.fromEpochSeconds(0L, 0), |
| 27 | + Instant.fromEpochSeconds(0L, 1), |
| 28 | + Instant.fromEpochSeconds(0L, 2), |
| 29 | + Instant.fromEpochSeconds(0L, 999999999), |
| 30 | + Instant.fromEpochSeconds(1L, 0), |
| 31 | + Instant.fromEpochSeconds(2L, 0) |
| 32 | + ) |
| 33 | + for (i in instants.indices) { |
| 34 | + val a = instants[i] |
| 35 | + for (j in instants.indices) { |
| 36 | + val b = instants[j] |
| 37 | + when { |
| 38 | + i < j -> { |
| 39 | + assertTrue(a < b, "$a <=> $b") |
| 40 | + assertNotEquals(a, b, "$a <=> $b") |
| 41 | + } |
| 42 | + i > j -> { |
| 43 | + assertTrue(a > b, "$a <=> $b") |
| 44 | + assertNotEquals(a, b, "$a <=> $b") |
| 45 | + } |
| 46 | + else -> { |
| 47 | + assertEquals(0, a.compareTo(b), "$a <=> $b") |
| 48 | + assertEquals(a, b, "$a <=> $b") |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun instantEquals() { |
| 57 | + val test5a: Instant = Instant.fromEpochSeconds(5L, 20) |
| 58 | + val test5b: Instant = Instant.fromEpochSeconds(5L, 20) |
| 59 | + val test5n: Instant = Instant.fromEpochSeconds(5L, 30) |
| 60 | + val test6: Instant = Instant.fromEpochSeconds(6L, 20) |
| 61 | + assertEquals(true, test5a == test5a) |
| 62 | + assertEquals(true, test5a == test5b) |
| 63 | + assertEquals(false, test5a == test5n) |
| 64 | + assertEquals(false, test5a == test6) |
| 65 | + assertEquals(true, test5b == test5a) |
| 66 | + assertEquals(true, test5b == test5b) |
| 67 | + assertEquals(false, test5b == test5n) |
| 68 | + assertEquals(false, test5b == test6) |
| 69 | + assertEquals(false, test5n == test5a) |
| 70 | + assertEquals(false, test5n == test5b) |
| 71 | + assertEquals(true, test5n == test5n) |
| 72 | + assertEquals(false, test5n == test6) |
| 73 | + assertEquals(false, test6 == test5a) |
| 74 | + assertEquals(false, test6 == test5b) |
| 75 | + assertEquals(false, test6 == test5n) |
| 76 | + assertEquals(true, test6 == test6) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun toEpochMilliseconds() { |
| 81 | + assertEquals(Instant.fromEpochSeconds(1L, 1000000).toEpochMilliseconds(), 1001L) |
| 82 | + assertEquals(Instant.fromEpochSeconds(1L, 2000000).toEpochMilliseconds(), 1002L) |
| 83 | + assertEquals(Instant.fromEpochSeconds(1L, 567).toEpochMilliseconds(), 1000L) |
| 84 | + assertEquals(Instant.fromEpochSeconds(Long.MAX_VALUE / 1_000_000).toEpochMilliseconds(), Long.MAX_VALUE / 1_000_000 * 1000) |
| 85 | + assertEquals(Instant.fromEpochSeconds(Long.MIN_VALUE / 1_000_000).toEpochMilliseconds(), Long.MIN_VALUE / 1_000_000 * 1000) |
| 86 | + assertEquals(Instant.fromEpochSeconds(0L, -1000000).toEpochMilliseconds(), -1L) |
| 87 | + assertEquals(Instant.fromEpochSeconds(0L, 1000000).toEpochMilliseconds(), 1) |
| 88 | + assertEquals(Instant.fromEpochSeconds(0L, 999999).toEpochMilliseconds(), 0) |
| 89 | + assertEquals(Instant.fromEpochSeconds(0L, 1).toEpochMilliseconds(), 0) |
| 90 | + assertEquals(Instant.fromEpochSeconds(0L, 0).toEpochMilliseconds(), 0) |
| 91 | + assertEquals(Instant.fromEpochSeconds(0L, -1).toEpochMilliseconds(), -1L) |
| 92 | + assertEquals(Instant.fromEpochSeconds(0L, -999999).toEpochMilliseconds(), -1L) |
| 93 | + assertEquals(Instant.fromEpochSeconds(0L, -1000000).toEpochMilliseconds(), -1L) |
| 94 | + assertEquals(Instant.fromEpochSeconds(0L, -1000001).toEpochMilliseconds(), -2L) |
| 95 | + } |
| 96 | +} |
0 commit comments