55
66package org.jetbrains.letsPlot.intern.standardizing
77
8+ import kotlinx.datetime.TimeZone
9+ import kotlinx.datetime.toInstant
810import org.jetbrains.letsPlot.commons.values.Color
9- import java.time.*
10- import java.util.*
1111import kotlin.test.*
1212import kotlinx.datetime.Instant as KInstant
1313import kotlinx.datetime.LocalDate as KLocalDate
@@ -17,20 +17,20 @@ import org.jetbrains.letsPlot.commons.intern.datetime.Date as LPDate
1717import org.jetbrains.letsPlot.commons.intern.datetime.DateTime as LPDateTime
1818import org.jetbrains.letsPlot.commons.intern.datetime.Month as LPMonth
1919import org.jetbrains.letsPlot.commons.intern.datetime.Time as LPTime
20- import org.jetbrains.letsPlot.commons.intern.datetime.TimeZone as LPTZ
20+ import org.jetbrains.letsPlot.commons.intern.datetime.TimeZone as LPTimeZone
2121
2222
2323class StandardizingTest {
2424
2525 @Test
26- fun `Numeric values standardized to a Double` () {
26+ fun numeric_values_standardized_to_a_Double () {
2727 val numericValues = listOf (
2828 1 .toByte(), // Byte
2929 2 .toShort(), // Short
3030 2 , // Int
3131 4L , // Long
32- 5.1f , // Float
33- 6.2 // Double
32+ 5.1f , // Float
33+ 6.2 // Double
3434 )
3535 val expected = numericValues.map { it.toDouble() }
3636
@@ -39,74 +39,62 @@ class StandardizingTest {
3939 }
4040
4141 @Test
42- fun `Temporal values standardized to a Double` () {
42+ fun temporal_values_standardized_to_a_Double () {
4343 // Expected timestamp for 2023-01-01T12:30:45Z
4444 val expectedTimestamp = 1672576245000L
4545
46- val expectedLocalDateTimestamp = ZonedDateTime .of(2023 , 1 , 1 , 0 , 0 , 0 , 0 , ZoneId .of(" UTC" ))
47- .toInstant().toEpochMilli()
46+ val expectedLocalDateTimestamp = KLocalDateTime (2023 , 1 , 1 , 0 , 0 , 0 , 0 )
47+ .toInstant(TimeZone .UTC )
48+ .toEpochMilliseconds()
4849
49- val expectedLocalTimeTimestamp = ZonedDateTime .of(1970 , 1 , 1 , 12 , 30 , 45 , 0 , ZoneId .of(" UTC" ))
50- .toInstant().toEpochMilli()
51-
52- // Java time API
53- val zonedDateTime = ZonedDateTime .of(2023 , 1 , 1 , 12 , 30 , 45 , 0 , ZoneId .of(" UTC" ))
54- val instant = zonedDateTime.toInstant()
55- val localDate = zonedDateTime.toLocalDate()
56- val localTime = zonedDateTime.toLocalTime()
57- val localDateTime = zonedDateTime.toLocalDateTime()
58- val date = Date .from(instant)
50+ val expectedLocalTimeTimestamp = KLocalDateTime (1970 , 1 , 1 , 12 , 30 , 45 , 0 )
51+ .toInstant(TimeZone .UTC )
52+ .toEpochMilliseconds()
5953
6054 // Kotlinx.datetime API
6155 val kInstant = KInstant .fromEpochMilliseconds(expectedTimestamp)
6256 val kLocalDate = KLocalDate (2023 , 1 , 1 )
6357 val kLocalTime = KLocalTime (12 , 30 , 45 )
6458 val kLocalDateTime = KLocalDateTime (2023 , 1 , 1 , 12 , 30 , 45 )
6559
66- val timestampValues = listOf (
67- zonedDateTime,
68- instant,
69- localDate,
70- localTime,
71- localDateTime,
72- date,
73-
60+ val values = listOf (
7461 kInstant,
7562 kLocalDate,
7663 kLocalTime,
7764 kLocalDateTime,
7865 )
7966
80- val timestampValuesStandardized = SeriesStandardizing .toList(timestampValues)
81- timestampValues.zip(timestampValuesStandardized).forEach { (input, result) ->
82- assertTrue(
83- result is Double ,
84- " Input: $input , ${input.javaClass.name} , result expected Double but was: ${result?.javaClass?.name} "
85- )
86-
87- val expected = when (input) {
88- is LocalDate ,
89- is KLocalDate -> expectedLocalDateTimestamp.toDouble()
90-
91- is LocalTime ,
92- is KLocalTime -> expectedLocalTimeTimestamp.toDouble()
93-
94- is LocalDateTime ,
67+ val expectedValues = values.map {
68+ when (it) {
69+ is KLocalDate -> expectedLocalDateTimestamp.toDouble() // Same as ZonedDateTime because here we use UTC
70+ is KLocalTime -> expectedLocalTimeTimestamp.toDouble() // Same as ZonedDateTime because here we use UTC
9571 is KLocalDateTime -> expectedTimestamp.toDouble() // Same as ZonedDateTime because here we use UTC
96-
9772 else -> expectedTimestamp.toDouble()
9873 }
74+ } + StandardizingTestJvmValues .getExpectedValues()
75+
9976
100- assertEquals(expected, result, " Input: $input , ${input.javaClass.name} " )
77+ val standardizedValues = SeriesStandardizing .toList(values + StandardizingTestJvmValues .getTestValues())
78+ values.zip(expectedValues).zip(standardizedValues) { (a, b), c ->
79+ Triple (a, b, c)
10180 }
81+ .forEach { (input, expected, result) ->
82+ // Expect all results to be Double
83+ assertTrue(
84+ result is Double ,
85+ " Input: $input , ${input::class } , result expected Double but was: ${result!! ::class } "
86+ )
87+
88+ assertEquals(expected, result, " Input: $input , ${input::class } " )
89+ }
10290 }
10391
10492 @Test
105- fun `LP internal datetime values are not supported` () {
93+ fun lp_internal_datetime_values_are_not_supported () {
10694 val date = LPDate (1 , LPMonth .JANUARY , 2023 )
10795 val time = LPTime (12 , 30 , 45 )
10896 val dateTime = LPDateTime (date, time)
109- val instant = dateTime.toInstant(LPTZ (" UTC" ))
97+ val instant = dateTime.toInstant(LPTimeZone (" UTC" ))
11098
11199 assertFailsWith<IllegalArgumentException > {
112100 Standardizing .standardizeValue(instant)
@@ -124,17 +112,12 @@ class StandardizingTest {
124112
125113
126114 @Test
127- fun `Collection of mixed values` () {
128- val dateTime = ZonedDateTime .of(
129- 2020 ,
130- 6 ,
131- 10 ,
132- 13 ,
133- 58 ,
134- 0 ,
135- 0 ,
136- ZoneId .of(" EST" , ZoneId .SHORT_IDS )
137- )
115+ fun a_collection_of_mixed_values () {
116+ // LocalDateTime in EST (UTC-5)
117+ val localDateTime = KLocalDateTime (2020 , 6 , 10 , 13 , 58 )
118+ // val timeZone = TimeZone.of("UTC-05:00")
119+ val timeZone = TimeZone .of(" UTC" )
120+ val instant = localDateTime.toInstant(timeZone)
138121
139122
140123 val values = listOf (
@@ -145,10 +128,8 @@ class StandardizingTest {
145128 Double .NaN , Double .NEGATIVE_INFINITY , Double .POSITIVE_INFINITY ,
146129 State .Idle ,
147130 Color .WHITE ,
148- java.awt.Color .WHITE ,
149- dateTime,
150- dateTime.toInstant(),
151- Date (dateTime.toInstant().toEpochMilli()),
131+ instant,
132+ localDateTime,
152133 )
153134
154135 val expected = listOf (
@@ -159,14 +140,12 @@ class StandardizingTest {
159140 null , null , null ,
160141 " Idle" ,
161142 " #ffffff" ,
162- " #ffffff" ,
163- 1 .59181548E12,
164- 1 .59181548E12,
165- 1 .59181548E12
143+ 1591797480000L .toDouble(),
144+ 1591797480000L .toDouble(),
166145 )
167146
168147 val result = SeriesStandardizing .toList(values)
169- assertEquals (expected, result)
148+ assertContentEquals (expected, result)
170149 }
171150}
172151
0 commit comments