RegressionTests__TimeDurationTest.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Jul 2019 18:53:03 +0200
changeset 2327 bf482d49aeaf
parent 2245 bfbf845df5cf
permissions -rw-r--r--
#QUALITY by exept class: RegressionTests::StringTests added: #test82c_expanding

"{ Encoding: utf8 }"

"{ Package: 'stx:goodies/regression' }"

"{ NameSpace: RegressionTests }"

TestCase subclass:#TimeDurationTest
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'tests-Regression-DateAndTime'
!


!TimeDurationTest methodsFor:'Testing'!

test_00_Basic
    |t|

    t := TimeDuration fromSeconds:1.
    self assert:(t asSeconds = 1).
    self assert:(t asMilliseconds == 1000).
    self assert:(t asMicroseconds = 1000000).
    self assert:(t asNanoseconds = 1000000000).
    self assert:(t asPicoseconds = 1000000000000).

    t := TimeDuration fromSeconds:1.5.
    self assert:(t asSeconds = 1).
    self assert:(t asTruncatedSeconds == 1).
    self assert:(t asExactSeconds = 1.5).
    self assert:(t asMilliseconds == 1500).
    self assert:(t asMicroseconds = 1500000).
    self assert:(t asNanoseconds = 1500000000).
    self assert:(t asPicoseconds = 1500000000000).

    t := TimeDuration fromMilliseconds:345.
    self assert:(t asSeconds = 0).
    self assert:(t asExactSeconds = 0.345).
    self assert:(t asTruncatedSeconds = 0).
    self assert:(t asMilliseconds == 345).
    self assert:(t asMicroseconds = 345000).
    self assert:(t asNanoseconds = 345000000).
    self assert:(t asPicoseconds = 345000000000).

    t := TimeDuration fromMicroseconds:345.
    self assert:(t asExactSeconds = 0.000345).
    self assert:(t asTruncatedSeconds = 0).
    self assert:(t asExactMilliseconds = 0.345).
    self assert:(t asTruncatedMilliseconds == 0).
    self assert:(t asMicroseconds = 345).
    self assert:(t asNanoseconds = 345000).
    self assert:(t asPicoseconds = 345000000).

    t := TimeDuration fromNanoseconds:345.
    self assert:(t asExactSeconds = 0.000000345).
    self assert:(t asTruncatedSeconds = 0).
    self assert:(t asTruncatedMilliseconds == 0).
    self assert:(t asExactMilliseconds = 0.000345).
    self assert:(t asTruncatedMicroseconds = 0).
    self assert:(t asExactMicroseconds = 0.345).
    self assert:(t asNanoseconds = 345).
    self assert:(t asPicoseconds = 345000).

    t := TimeDuration fromPicoseconds:345.
    self assert:(t asExactSeconds = 0.000000000345).
    self assert:(t asTruncatedSeconds = 0).
    self assert:(t asExactMilliseconds = 0.000000345).
    self assert:(t asTruncatedMilliseconds == 0).
    self assert:(t asExactMicroseconds = 0.000345).
    self assert:(t asTruncatedMicroseconds = 0).
    self assert:(t asExactNanoseconds = 0.345).
    self assert:(t asTruncatedNanoseconds = 0).
    self assert:(t asPicoseconds = 345).

    t := TimeDuration fromMicroseconds:-10001.
    self assert:(t asExactMicroseconds = -10001).

    (4 to:4000000000000000000 byFactor:1000) do:[:v |
        self assert:(t := TimeDuration new setPicoseconds:v) asPicoseconds = v.
        self assert:(t := TimeDuration fromPicoseconds:v) asPicoseconds = v.
        self assert:(t := v picoseconds) asPicoseconds = v.

        self assert:(t := TimeDuration new setNanoseconds:v) asNanoseconds = v.
        self assert:(t := TimeDuration fromNanoseconds:v) asNanoseconds = v.
        self assert:(t := v nanoseconds) asNanoseconds = v.

    ].

    self assert:(t := 4 picoseconds) printString = ('4ps').
    self assert:(t := 40 picoseconds) printString = ('40ps').
    self assert:(t := 400 picoseconds) printString = ('400ps').
    self assert:(t := 4000 picoseconds) printString = ('4ns').
    self assert:(4000 picoseconds = 4 nanoseconds).

    self assert:(t := 4 nanoseconds) printString = ('4ns').
    self assert:(t := 40 nanoseconds) printString = ('40ns').
    self assert:(t := 400 nanoseconds) printString = ('400ns').
    self assert:(t := 4000 nanoseconds) printString = ('4µs').
    self assert:(4000 nanoseconds = 4 microseconds).

    self assert:(t := 4 microseconds) printString = ('4µs').
    self assert:(t := 40 microseconds) printString = ('40µs').
    self assert:(t := 400 microseconds) printString = ('400µs').
    self assert:(t := 4000 microseconds) printString = ('4ms').
    self assert:(4000 microseconds = 4 milliseconds).

    self assert:(t := 4 milliseconds) printString = ('4ms').
    self assert:(t := 40 milliseconds) printString = ('40ms').
    self assert:(t := 400 milliseconds) printString = ('400ms').
    self assert:(t := 4000 milliseconds) printString = ('4s').
    self assert:(4000 milliseconds = 4 seconds).
    "
     self new test_00_Basic
    "

    "Modified: / 24-05-2018 / 17:42:49 / Claus Gittinger"
!

test_01_Addition
    |t1 t2 sum|

    t1 := TimeDuration fromSeconds:1.
    t2 := TimeDuration fromMilliseconds:1.
    sum := t1 + t2.

    self assert:(sum asMilliseconds == 1001).
    self assert:(sum asMicroseconds = 1001000).
    self assert:(sum asNanoseconds = 1001000000).
    self assert:(sum asPicoseconds = 1001000000000).


    t1 := TimeDuration fromSeconds:1.
    t2 := TimeDuration fromMicroseconds:1.
    sum := t1 + t2.

    self assert:(sum asMilliseconds == 1000).
    self assert:(sum asMicroseconds = 1000001).
    self assert:(sum asNanoseconds = 1000001000).
    self assert:(sum asPicoseconds = 1000001000000).

    t1 := TimeDuration fromNanoseconds:100.
    t2 := TimeDuration fromNanoseconds:15.5.
    sum := t1 + t2.

    self assert:(sum asNanoseconds = 115).
    self assert:(sum asPicoseconds = 115500).


    "
     self new test_01_Addition
    "
!

test_02_Subtraction
    |t1 t2 sum|

    t1 := TimeDuration fromSeconds:1.
    t2 := TimeDuration fromMilliseconds:1.
    sum := t1 - t2.

    self assert:(sum asMilliseconds == 999).
    self assert:(sum asMicroseconds = 999000).
    self assert:(sum asNanoseconds = 999000000).
    self assert:(sum asPicoseconds = 999000000000).


    t1 := TimeDuration fromSeconds:1.
    t2 := TimeDuration fromMicroseconds:1.
    sum := t1 - t2.

    self assert:(sum asMilliseconds == 999).
    self assert:(sum asMicroseconds = 999999).
    self assert:(sum asNanoseconds = 999999000).
    self assert:(sum asPicoseconds = 999999000000).

    t1 := TimeDuration fromNanoseconds:100.
    t2 := TimeDuration fromNanoseconds:15.5.
    sum := t1 - t2.

    self assert:(sum asNanoseconds = 84).
    self assert:(sum asPicoseconds = 84500).


    "
     self new test_01_Subtraction
    "
!

test_03_Multiplication
    |t rslt|

    t := TimeDuration fromSeconds:1.
    rslt := t * 5.
    self assert:(rslt asMilliseconds == 5000).
    self assert:(rslt asPicoseconds = 5000000000000).

    rslt := 5 * t.
    self assert:(rslt asMilliseconds == 5000).
    self assert:(rslt asPicoseconds = 5000000000000).


    t := TimeDuration fromSeconds:1.1.
    rslt := t * 2.
    self assert:(rslt asMilliseconds == 2200).
    self assert:(rslt asPicoseconds = 2200000000000).

    rslt := 2 * t.
    self assert:(rslt asMilliseconds == 2200).
    self assert:(rslt asPicoseconds = 2200000000000).


    t := TimeDuration fromNanoseconds:100.
    rslt := t * 4.5.
    self assert:(rslt asNanoseconds = 450).
    self assert:(rslt asPicoseconds = 450000).

    rslt := 4.5 * t.
    self assert:(rslt asNanoseconds = 450).
    self assert:(rslt asPicoseconds = 450000).


    t := TimeDuration fromMilliseconds:100.
    t := t + 50 nanoseconds.

    rslt := t * 5.
    self assert:(rslt asNanoseconds = 500000250).

    rslt := 5 * t.
    self assert:(rslt asNanoseconds = 500000250).


    "
     self new test_03_Multiplication
    "
!

test_04_Division
    |t rslt|

    t := TimeDuration fromSeconds:1.
    rslt := t / 5.
    self assert:(rslt asMilliseconds == 200).
    self assert:(rslt asPicoseconds = 200000000000).


    t := TimeDuration fromSeconds:2.2.
    rslt := t / 2.
    self assert:(rslt asMilliseconds == 1100).
    self assert:(rslt asPicoseconds = 1100000000000).


    t := TimeDuration fromNanoseconds:450.
    rslt := t / 4.5.
    self assert:(rslt asNanoseconds = 100).
    self assert:(rslt asPicoseconds = 100000).


    t := TimeDuration fromMilliseconds:100.
    t := t + 50 nanoseconds.

    rslt := t / 5.
    self assert:(rslt asNanoseconds = 20000010).


    "
     self new test_04_Division
    "
!

test_05_Division    
    "dividing two timeDurations gives a numeric quotient"

    |t1 t2 rslt|

    t1 := TimeDuration fromSeconds:1.
    t2 := TimeDuration fromSeconds:2.
    rslt := t1 / t2.
    self assert:(rslt isNumber).
    self assert:(rslt = 0.5).

    t1 := TimeDuration fromSeconds:1.
    t2 := TimeDuration fromMilliseconds:1.
    rslt := t1 / t2.
    self assert:(rslt isNumber).
    self assert:(rslt = 1000).

    t1 := TimeDuration fromMilliseconds:1.
    t2 := TimeDuration fromMicroseconds:1.
    rslt := t1 / t2.
    self assert:(rslt isNumber).
    self assert:(rslt = 1000).

    t1 := TimeDuration fromMicroseconds:1.
    t2 := TimeDuration fromNanoseconds:1.
    rslt := t1 / t2.
    self assert:(rslt isNumber).
    self assert:(rslt = 1000).

    t1 := TimeDuration fromSeconds:1.
    t2 := TimeDuration fromNanoseconds:1.
    rslt := t1 / t2.
    self assert:(rslt isNumber).
    self assert:(rslt = 1000000000).

    t1 := TimeDuration fromSeconds:1.
    t2 := TimeDuration fromNanoseconds:50.
    rslt := t1 / t2.
    self assert:(rslt isNumber).
    self assert:(rslt = 20000000).


    "
     self new test_05_Division
    "
!

test_06_miscArithmetic    
    |t rslt|

    t := TimeDuration fromSeconds:1.
    rslt := t negated.
    self assert:(rslt asSeconds = -1).
    self assert:(rslt = (TimeDuration fromSeconds:-1)).

    t := TimeDuration fromMicroseconds:1.
    rslt := t negated.
    self assert:(rslt asMicroseconds = -1).
    self assert:(rslt = (TimeDuration fromMicroseconds:-1)).

    t := TimeDuration fromMicroseconds:10001.
    rslt := t negated.
    self assert:(rslt asMicroseconds = -10001).
    self assert:(rslt = (TimeDuration fromMicroseconds:-10001)).
    rslt := rslt negated.
    self assert:(rslt asMicroseconds = 10001).
    self assert:(rslt = (TimeDuration fromMicroseconds:10001)).


    t := TimeDuration fromMicroseconds:-10001.
    rslt := t abs.
    self assert:(rslt asMicroseconds = 10001).
    self assert:(rslt = (TimeDuration fromMicroseconds:10001)).

    "
     self new test_06_miscArithmetic
    "
!

test_09_Reading
    |t1 t2|

    t2 := TimeDuration fromString:'1.001s'.
    self assert:(t2 asMilliseconds == 1001).

    t1 := TimeDuration fromString:'1001ms'.
    self assert:(t1 asMilliseconds = 1001).

    t1 := TimeDuration fromString:'1001ns'.
    self assert:(t1 asNanoseconds = 1001).

    t1 := TimeDuration fromString:'1001us'.
    self assert:(t1 asMicroseconds = 1001).

    t1 := TimeDuration fromString:'1001ps'.
    self assert:(t1 asPicoseconds = 1001).

    t1 := TimeDuration fromString:'1s 30us'.
    self assert:(t1 asMicroseconds = 1000030).

    t1 := TimeDuration fromString:'0.1s'.
    self assert:(t1 asMilliseconds = 100).
    t1 := TimeDuration fromString:'0.1ms'.
    self assert:(t1 asMicroseconds = 100).
    t1 := TimeDuration fromString:'0.1us'.
    self assert:(t1 asNanoseconds = 100).
    t1 := TimeDuration fromString:'0.1ns'.
    self assert:(t1 asPicoseconds = 100).

    t1 := TimeDuration fromString:'0,1ns'.
    self assert:(t1 asPicoseconds = 100).

    t1 := TimeDuration fromString:'01:30:33'.
    self assert:(t1 asSeconds = ((1*3600)+(30*60)+33)).
    self assert:(t1 asMilliseconds = (((1*3600)+(30*60)+33)*1000)).

    t1 := TimeDuration fromString:'01:30:33.5'.
    self assert:(t1 asSeconds = ((1*3600)+(30*60)+33)).
    self assert:(t1 asMilliseconds = ((((1*3600)+(30*60)+33)*1000)+500)).

    t1 := TimeDuration fromString:'01:30:33,5'.
    self assert:(t1 asSeconds = ((1*3600)+(30*60)+33)).
    self assert:(t1 asMilliseconds = ((((1*3600)+(30*60)+33)*1000)+500)).

    t1 := TimeDuration fromString:'01:30:33,05'.
    self assert:(t1 asSeconds = ((1*3600)+(30*60)+33)).
    self assert:(t1 asMilliseconds = ((((1*3600)+(30*60)+33)*1000)+50)).

    t1 := TimeDuration fromString:'01:30:33,005'.
    self assert:(t1 asSeconds = ((1*3600)+(30*60)+33)).
    self assert:(t1 asMilliseconds = ((((1*3600)+(30*60)+33)*1000)+5)).

    t1 := TimeDuration fromString:'01:30:33,0005'.
    self assert:(t1 asSeconds = ((1*3600)+(30*60)+33)).
    self assert:(t1 asMicroseconds = ((((1*3600)+(30*60)+33)*1000*1000)+500)).

    t1 := TimeDuration fromString:'01:30:33,00005'.
    self assert:(t1 asSeconds = ((1*3600)+(30*60)+33)).
    self assert:(t1 asMicroseconds = ((((1*3600)+(30*60)+33)*1000*1000)+50)).

    t1 := TimeDuration fromString:'01:30:33,000005'.
    self assert:(t1 asSeconds = ((1*3600)+(30*60)+33)).
    self assert:(t1 asMicroseconds = ((((1*3600)+(30*60)+33)*1000*1000)+5)).

    t1 := TimeDuration fromString:'01:30:33,0000005'.
    self assert:(t1 asSeconds = ((1*3600)+(30*60)+33)).
    self assert:(t1 asNanoseconds = ((((1*3600)+(30*60)+33)*1000*1000*1000)+500)).

    t1 := TimeDuration fromString:'01:30:33,00000005'.
    self assert:(t1 asSeconds = ((1*3600)+(30*60)+33)).
    self assert:(t1 asNanoseconds = ((((1*3600)+(30*60)+33)*1000*1000*1000)+50)).

    t1 := TimeDuration fromString:'01:30:33,000000005'.
    self assert:(t1 asSeconds = ((1*3600)+(30*60)+33)).
    self assert:(t1 asNanoseconds = ((((1*3600)+(30*60)+33)*1000*1000*1000)+5)).

    t1 := TimeDuration fromString:'0.000000005'.
    self assert:(t1 asNanoseconds = 5).

    t1 := TimeDuration fromString:'0.000000005s'.
    self assert:(t1 asNanoseconds = 5).

    t1 := TimeDuration fromString:'1d 5h'.
    self assert:(t1 asTruncatedHours = 29).

    t1 := TimeDuration fromString:'1d 5h 30m'.
    self assert:(t1 asTruncatedHours = 29).
    self assert:(t1 asExactHours = 29.5).

    "
     self new test_09_Reading
    "
!

test_10_PrintingReading
     #(
        "/ hours minutes seconds millis
        (0 0 0 0)
        (0 0 0 1)
        (0 0 0 9)
        (0 0 0 10)
        (0 0 0 11)
        (0 0 0 99)
        (0 0 0 100)
        (0 0 0 101)
        (0 0 0 999)
        (0 0 0 1000)
        (0 0 0 1001)

        (0 33 0 123)
        (2 0 0 123)
        (2 33 0 123)
        (100 33 0 123)
        (10000 33 0 123)
        (1000000 33 0 123)

        (2 33 0 0)
        (2 0 0 0)
        (24 0 0 0)
        (0 0 0 123)
    ) do:[:hmsms |
        |h m s ms d1 d2 s1 s2|

        h := hmsms first.
        m := hmsms second.
        s := hmsms third.
        ms := hmsms fourth.
        "/ generate a duration
        d1 := TimeDuration hours:h minutes:m seconds:s milliseconds:ms.
        "/ print it
        s1 := d1 printString.
        "/ read it back
        d2 := TimeDuration readFrom:s1.
        s2 := d2 printString.
        "/ same time?
        self assert:(d1 = d2).
        self assert:(d1 timeEncoding = d2 timeEncoding).
        "/ same string?
        self assert:(s1 = s2).
     ].


"/     (TimeDuration hours:0 minutes:0 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'
"/     (TimeDuration hours:0 minutes:0 seconds:10 milliseconds:123) printStringFormat:'%h:%m:%s'
"/     (TimeDuration hours:0 minutes:33 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'
"/     (TimeDuration hours:2 minutes:33 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'
"/     (TimeDuration hours:100 minutes:33 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'
"/     (TimeDuration hours:10000 minutes:33 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'
"/     (TimeDuration hours:1000000 minutes:33 seconds:0 milliseconds:123) printStringFormat:'%h:%m:%s'

    "
     self new test_10_PrintingReading
    "

    "Created: / 18-07-2007 / 14:02:01 / cg"
!

test_11_Comparing
    |t1 t2|

    t1 := TimeDuration fromSeconds:1.
    t2 := t1 + 1 picoseconds.
    self assert:(t2 > t1).
    self assert:(t2 >= t1).
    self assert:(t2 = t1) not.
    self assert:(t2 <= t1) not.
    self assert:(t2 < t1) not.

    t1 := TimeDuration fromSeconds:1.
    t2 := t1 - 1 picoseconds.
    self assert:(t2 < t1).
    self assert:(t2 <= t1).
    self assert:(t2 = t1) not.
    self assert:(t2 >= t1) not.
    self assert:(t2 > t1) not.

    t1 := TimeDuration fromSeconds:0.001.   "/ 1 milli
    t2 := TimeDuration fromSeconds:0.0009.  "/ 900 micros
    self assert:(t2 < t1).
    self assert:(t2 <= t1).
    self assert:(t2 = t1) not.
    self assert:(t2 >= t1) not.
    self assert:(t2 > t1) not.

    t1 := TimeDuration fromSeconds:0.0001.   "/ 100 micros
    t2 := TimeDuration fromSeconds:0.00009.  "/ 90 micros
    self assert:(t2 < t1).
    self assert:(t2 <= t1).
    self assert:(t2 = t1) not.
    self assert:(t2 >= t1) not.
    self assert:(t2 > t1) not.

    t1 := TimeDuration fromSeconds:0.00001.   "/ 10 micros
    t2 := TimeDuration fromSeconds:0.000009.  "/ 9 micros
    self assert:(t2 < t1).
    self assert:(t2 <= t1).
    self assert:(t2 = t1) not.
    self assert:(t2 >= t1) not.
    self assert:(t2 > t1) not.

    t1 := TimeDuration fromSeconds:0.000001.   "/ 1 micros
    t2 := TimeDuration fromSeconds:0.0000009.  "/ 900 nanos
    self assert:(t2 < t1).
    self assert:(t2 <= t1).
    self assert:(t2 = t1) not.
    self assert:(t2 >= t1) not.
    self assert:(t2 > t1) not.

    t1 := TimeDuration fromSeconds:0.0000001.   "/ 100 nanos
    t2 := TimeDuration fromSeconds:0.00000009.  "/ 90 nanos
    self assert:(t2 < t1).
    self assert:(t2 <= t1).
    self assert:(t2 = t1) not.
    self assert:(t2 >= t1) not.
    self assert:(t2 > t1) not.

    t1 := TimeDuration fromSeconds:0.00000001.   "/ 10 nanos
    t2 := TimeDuration fromSeconds:0.000000009.  "/ 9 nanos
    self assert:(t2 < t1).
    self assert:(t2 <= t1).
    self assert:(t2 = t1) not.
    self assert:(t2 >= t1) not.
    self assert:(t2 > t1) not.

    t1 := TimeDuration fromSeconds:0.000000001.   "/ 1 nanos
    t2 := TimeDuration fromSeconds:0.0000000009.  "/ 900 picos
    self assert:(t2 < t1).
    self assert:(t2 <= t1).
    self assert:(t2 = t1) not.
    self assert:(t2 >= t1) not.
    self assert:(t2 > t1) not.

    t1 := TimeDuration fromSeconds:0.0000000001.   "/ 100 picos
    t2 := TimeDuration fromSeconds:0.00000000009.  "/ 90 picos
    self assert:(t2 < t1).
    self assert:(t2 <= t1).
    self assert:(t2 = t1) not.
    self assert:(t2 >= t1) not.
    self assert:(t2 > t1) not.

    t1 := TimeDuration fromSeconds:0.00000000001.   "/ 10 picos
    t2 := TimeDuration fromSeconds:0.000000000009.  "/ 9 picos
    self assert:(t2 < t1).
    self assert:(t2 <= t1).
    self assert:(t2 = t1) not.
    self assert:(t2 >= t1) not.
    self assert:(t2 > t1) not.

    t1 := TimeDuration fromSeconds:0.000000000001.   "/ 1 picos
    t2 := TimeDuration fromSeconds:0.0000000000009.  "/ 0.9 picos
    self assert:(t2 < t1).
    self assert:(t2 <= t1).
    self assert:(t2 = t1) not.
    self assert:(t2 >= t1) not.
    self assert:(t2 > t1) not.

    "/ below the picosecond resolution
    "/ these will compare as equal
    "/ t1 := TimeDuration fromSeconds:0.0000000000001.   "/ 0.1 picos
    "/ t2 := TimeDuration fromSeconds:0.00000000000009.  "/ 0.09 picos
    "/ self assert:(t2 < t1).
    "/ self assert:(t2 <= t1).
    "/ self assert:(t2 = t1) not.
    "/ self assert:(t2 >= t1) not.
    "/ self assert:(t2 > t1) not.

    "
     self new test_11_Comparing
    "

    "Modified: / 26-05-2019 / 10:14:30 / Claus Gittinger"
!

test_12_Creation
    |t1 t2|

    t1 := TimeDuration fromSeconds:0.001.
    t2 := TimeDuration fromMilliseconds:1.
    self assert:(t2 = t1).

    "
     self new test_12_Creation
    "

    "Created: / 26-05-2019 / 10:15:50 / Claus Gittinger"
! !

!TimeDurationTest class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !