|
| 1 | +package org.scalajs.testsuite.javalib.time |
| 2 | + |
| 3 | +import java.time.temporal.{ChronoField, ChronoUnit, UnsupportedTemporalTypeException} |
| 4 | +import java.time.{DateTimeException, Instant} |
| 5 | + |
| 6 | +import org.junit.Assert._ |
| 7 | +import org.junit.Test |
| 8 | +import org.scalajs.testsuite.utils.AssertThrows |
| 9 | + |
| 10 | +class InstantTest extends TemporalTest[Instant]{ |
| 11 | + |
| 12 | + import AssertThrows._ |
| 13 | + import DateTimeTestUtil._ |
| 14 | + import Instant._ |
| 15 | + import ChronoField._ |
| 16 | + import ChronoUnit._ |
| 17 | + |
| 18 | + override def isSupported(unit: ChronoUnit): Boolean = unit == NANOS || unit == MICROS || unit == MILLIS || |
| 19 | + unit == SECONDS || unit == MINUTES || unit ==HOURS || unit == HALF_DAYS || unit == DAYS |
| 20 | + override def isSupported(field: ChronoField): Boolean = field == NANO_OF_SECOND || |
| 21 | + field == MICRO_OF_SECOND || field == MILLI_OF_SECOND || field == INSTANT_SECONDS |
| 22 | + |
| 23 | + override val samples: Seq[Instant] = Seq(MIN, MAX, EPOCH) |
| 24 | + |
| 25 | + @Test def test_getLong():Unit = { |
| 26 | + assertEquals(0L, MIN.getLong(NANO_OF_SECOND)) |
| 27 | + assertEquals(0L, MIN.getLong(MICRO_OF_SECOND)) |
| 28 | + assertEquals(0L, MIN.getLong(MILLI_OF_SECOND)) |
| 29 | + assertEquals(-31557014167219200L, MIN.getLong(INSTANT_SECONDS)) |
| 30 | + |
| 31 | + assertEquals(999999999L, MAX.getLong(NANO_OF_SECOND)) |
| 32 | + assertEquals(999999L, MAX.getLong(MICRO_OF_SECOND)) |
| 33 | + assertEquals(999L, MAX.getLong(MILLI_OF_SECOND)) |
| 34 | + assertEquals(31556889864403199L, MAX.getLong(INSTANT_SECONDS)) |
| 35 | + |
| 36 | + assertEquals(0L, EPOCH.getLong(NANO_OF_SECOND)) |
| 37 | + assertEquals(0L, EPOCH.getLong(MICRO_OF_SECOND)) |
| 38 | + assertEquals(0L, EPOCH.getLong(MILLI_OF_SECOND)) |
| 39 | + assertEquals(0L, EPOCH.getLong(INSTANT_SECONDS)) |
| 40 | + |
| 41 | + } |
| 42 | + @Test def test_now():Unit = { |
| 43 | + assertNotNull(now()) |
| 44 | + } |
| 45 | + |
| 46 | + @Test def test_ofEpochSecond():Unit = { |
| 47 | + testDateTime(ofEpochSecond(0))(EPOCH) |
| 48 | + testDateTime(ofEpochSecond(0, 0))(EPOCH) |
| 49 | + testDateTime(ofEpochSecond(31556889864403199L, 999999999))(MAX) |
| 50 | + testDateTime(ofEpochSecond(-31557014167219200L, 0))(MIN) |
| 51 | + testDateTime(ofEpochSecond(123))(ofEpochSecond(123,0)) |
| 52 | + testDateTime(ofEpochSecond(3, 1))(ofEpochSecond(4, -999999999)) |
| 53 | + testDateTime(ofEpochSecond(3, 1))(ofEpochSecond(2, 1000000001)) |
| 54 | + |
| 55 | + expectThrows(classOf[DateTimeException], ofEpochSecond(31556889864403199L + 1)) |
| 56 | + expectThrows(classOf[DateTimeException], ofEpochSecond(31556889864403199L, 999999999 + 1)) |
| 57 | + expectThrows(classOf[DateTimeException], ofEpochSecond(-31557014167219200L - 1)) |
| 58 | + expectThrows(classOf[DateTimeException], ofEpochSecond(-31557014167219200L - 1, -1)) |
| 59 | + } |
| 60 | + |
| 61 | + @Test def test_getNano():Unit = { |
| 62 | + assertEquals(0, EPOCH.getNano) |
| 63 | + assertEquals(0, MIN.getNano) |
| 64 | + assertEquals(999999999, MAX.getNano) |
| 65 | + assertEquals(1, ofEpochSecond(4, -999999999).getNano) |
| 66 | + assertEquals(1, ofEpochSecond(2, 1000000001).getNano) |
| 67 | + assertEquals(234567890, ofEpochSecond(1, 234567890).getNano) |
| 68 | + } |
| 69 | + |
| 70 | + @Test def test_with():Unit = { |
| 71 | + val supportedFields = Set(NANO_OF_SECOND, MICRO_OF_SECOND, MILLI_OF_SECOND, INSTANT_SECONDS) |
| 72 | + for (t <- samples) { |
| 73 | + for (n <- Seq(0, 999, 999999, 999999999)) |
| 74 | + testDateTime(t.`with`(NANO_OF_SECOND, n))(ofEpochSecond(t.getEpochSecond(), n)) |
| 75 | + for (n <- Seq(0, 999, 999999)) |
| 76 | + testDateTime(t.`with`(MICRO_OF_SECOND, n))(ofEpochSecond(t.getEpochSecond(), n * 1000)) |
| 77 | + for (n <- Seq(0, 500, 999)) |
| 78 | + testDateTime(t.`with`(MILLI_OF_SECOND, n))(ofEpochSecond(t.getEpochSecond(), n * 1000000)) |
| 79 | + for (n <- Seq(-1000000000L, -86400L, -3600L, -60L, -1L, 0L, 1L, 60L, 3600L, 86400L, 1000000000L)) |
| 80 | + testDateTime(t.`with`(INSTANT_SECONDS, n))(ofEpochSecond(n, t.getNano())) |
| 81 | + |
| 82 | + for (f <- ChronoField.values()) { |
| 83 | + if (!supportedFields(f)) expectThrows(classOf[UnsupportedTemporalTypeException], t.`with`(f, 1)) |
| 84 | + } |
| 85 | + |
| 86 | + expectThrows(classOf[DateTimeException], t.`with`(NANO_OF_SECOND, -1)) |
| 87 | + expectThrows(classOf[DateTimeException], t.`with`(NANO_OF_SECOND, 999999999+1)) |
| 88 | + expectThrows(classOf[DateTimeException], t.`with`(MICRO_OF_SECOND, -1)) |
| 89 | + expectThrows(classOf[DateTimeException], t.`with`(MICRO_OF_SECOND, 999999+1)) |
| 90 | + expectThrows(classOf[DateTimeException], t.`with`(MILLI_OF_SECOND, -1)) |
| 91 | + expectThrows(classOf[DateTimeException], t.`with`(MILLI_OF_SECOND, 999+1)) |
| 92 | + expectThrows(classOf[DateTimeException], t.`with`(INSTANT_SECONDS, MIN.getEpochSecond-1)) |
| 93 | + expectThrows(classOf[DateTimeException], t.`with`(INSTANT_SECONDS, MAX.getEpochSecond+1)) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Test def test_truncatedTo():Unit = { |
| 98 | + testDateTime(MIN.truncatedTo(NANOS))(MIN) |
| 99 | + testDateTime(MIN.truncatedTo(MICROS))(MIN) |
| 100 | + testDateTime(MIN.truncatedTo(MILLIS))(MIN) |
| 101 | + testDateTime(MIN.truncatedTo(SECONDS))(MIN) |
| 102 | + testDateTime(MIN.truncatedTo(MINUTES))(MIN) |
| 103 | + testDateTime(MIN.truncatedTo(HOURS))(MIN) |
| 104 | + testDateTime(MIN.truncatedTo(HALF_DAYS))(MIN) |
| 105 | + testDateTime(MIN.truncatedTo(DAYS))(MIN) |
| 106 | + |
| 107 | + testDateTime(MAX.truncatedTo(NANOS))(MAX) |
| 108 | + testDateTime(MAX.truncatedTo(MICROS))(MAX.minusNanos(999)) |
| 109 | + testDateTime(MAX.truncatedTo(MILLIS))(MAX.minusNanos(999999)) |
| 110 | + testDateTime(MAX.truncatedTo(SECONDS))(MAX.minusNanos(999999999)) |
| 111 | + testDateTime(MAX.truncatedTo(MINUTES))(MAX.minusNanos(999999999).minusSeconds(59)) |
| 112 | + testDateTime(MAX.truncatedTo(HOURS))(MAX.minusNanos(999999999).minusSeconds(59).minus(59, MINUTES)) |
| 113 | + testDateTime(MAX.truncatedTo(HALF_DAYS))(MAX.minusNanos(999999999).minusSeconds(59).minus(59, MINUTES).minus(11, HOURS)) |
| 114 | + testDateTime(MAX.truncatedTo(DAYS))(MAX.minusNanos(999999999).minusSeconds(59).minus(59, MINUTES).minus(23, HOURS)) |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + @Test def test_ofEpochMilli():Unit = { |
| 119 | + testDateTime(ofEpochMilli(0))(EPOCH) |
| 120 | + testDateTime(ofEpochMilli(1234))(ofEpochSecond(1,234000000)) |
| 121 | + } |
| 122 | + |
| 123 | + @Test def test_from():Unit = { |
| 124 | + for (t <- samples) |
| 125 | + testDateTime(from(t))(t) |
| 126 | + } |
| 127 | + |
| 128 | + @Test def test_compareTto():Unit = { |
| 129 | + assertEquals(0, MIN.compareTo(MIN)) |
| 130 | + assertEquals(0, MAX.compareTo(MAX)) |
| 131 | + assertTrue(MAX.compareTo(MIN) > 0) |
| 132 | + assertTrue(MIN.compareTo(MAX) < 0) |
| 133 | + } |
| 134 | + |
| 135 | + @Test def test_isAfter(): Unit = { |
| 136 | + assertFalse(MIN.isAfter(MIN)) |
| 137 | + assertFalse(MIN.isAfter(MAX)) |
| 138 | + assertTrue(MAX.isAfter(MIN)) |
| 139 | + assertFalse(MAX.isAfter(MAX)) |
| 140 | + } |
| 141 | + |
| 142 | + @Test def test_isBefore(): Unit = { |
| 143 | + assertFalse(MIN.isBefore(MIN)) |
| 144 | + assertTrue(MIN.isBefore(MAX)) |
| 145 | + assertFalse(MAX.isBefore(MIN)) |
| 146 | + assertFalse(MAX.isBefore(MAX)) |
| 147 | + } |
| 148 | + |
| 149 | + @Test def test_adjustInto():Unit = { |
| 150 | + for { |
| 151 | + t1 <- samples |
| 152 | + t2 <- samples |
| 153 | + } { |
| 154 | + testDateTime(t1.adjustInto(t2))(t1) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | + @Test def test_until(): Unit = { |
| 160 | + assertEquals(63113904031622399L, MIN.until(MAX, SECONDS) ) |
| 161 | + assertEquals(1051898400527039L, MIN.until(MAX, MINUTES)) |
| 162 | + assertEquals(17531640008783L, MIN.until(MAX, HOURS)) |
| 163 | + assertEquals(1460970000731L, MIN.until(MAX, HALF_DAYS)) |
| 164 | + assertEquals(730485000365L, MIN.until(MAX, DAYS)) |
| 165 | + |
| 166 | + for (u <- timeBasedUnits) { |
| 167 | + assertEquals(-MIN.until(MAX, u), MAX.until(MIN, u)) |
| 168 | + assertEquals(0L, MIN.until(MIN, u)) |
| 169 | + assertEquals(0L, MAX.until(MAX, u)) |
| 170 | + } |
| 171 | + for (u <- dateBasedUnits) { |
| 172 | + if (u != DAYS) |
| 173 | + expectThrows(classOf[UnsupportedTemporalTypeException], MIN.until(MIN, u)) |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @Test def test_plus():Unit = { |
| 178 | + val values = Seq(Long.MinValue, -1000000000L, -86400L, -3600L, -60L, -1L, 0L, |
| 179 | + 1L, 60L, 3600L, 86400L, 1000000000L, Long.MaxValue) |
| 180 | + |
| 181 | + for { |
| 182 | + t <- samples |
| 183 | + n <- values |
| 184 | + } { |
| 185 | + testDateTime(t.plus(n, NANOS))(t.plusNanos(n)) |
| 186 | + testDateTime(t.plus(n, MICROS))(t.plusNanos(n * 1000)) |
| 187 | + testDateTime(t.plus(n, MILLIS))(t.plusNanos(n * 1000000)) |
| 188 | + testDateTime(t.plus(n, SECONDS))(t.plusSeconds(n)) |
| 189 | + testDateTime(t.plus(n, MINUTES))(t.plusSeconds(n * 60)) |
| 190 | + testDateTime(t.plus(n, HOURS))(t.plusSeconds(n * 60 * 60)) |
| 191 | + testDateTime(t.plus(n, HALF_DAYS))(t.plusSeconds(n * 60 * 60 * 12)) |
| 192 | + testDateTime(t.plus(n, DAYS))(t.plusSeconds(n * 60 * 60 * 24)) |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments