mirror of https://github.com/waku-org/nwaku.git
35 lines
1.1 KiB
Nim
35 lines
1.1 KiB
Nim
{.used.}
|
|
|
|
import
|
|
testutils/unittests
|
|
import
|
|
../../waku/waku_core/time
|
|
|
|
suite "Waku Core - Time":
|
|
|
|
test "Test timestamp conversion":
|
|
## Given
|
|
let
|
|
nanoseconds = 1676562429123456789.int64
|
|
secondsPart = nanoseconds div 1_000_000_000
|
|
nanosecondsPart = nanoseconds mod 1_000_000_000
|
|
secondsFloat = secondsPart.float64 + (nanosecondsPart.float64 / 1_000_000_000.float64)
|
|
lowResTimestamp = Timestamp(secondsPart.int64 * 1_000_000_000.int64) # 1676562429000000000
|
|
highResTimestamp = Timestamp(secondsFloat * 1_000_000_000.float64) # 1676562429123456789
|
|
|
|
require highResTimestamp > lowResTimestamp # Sanity check
|
|
|
|
## When
|
|
let
|
|
timeInSecondsInt = secondsPart.int
|
|
timeInSecondsInt64 = secondsPart.int64
|
|
timeInSecondsFloat = float(secondsFloat)
|
|
timeInSecondsFloat64 = float64(secondsFloat)
|
|
|
|
## Then
|
|
check:
|
|
getNanosecondTime(timeInSecondsInt) == lowResTimestamp
|
|
getNanosecondTime(timeInSecondsInt64) == lowResTimestamp
|
|
getNanosecondTime(timeInSecondsFloat) == highResTimestamp
|
|
getNanosecondTime(timeInSecondsFloat64) == highResTimestamp
|