RegressionTests__TimeDurationTest.st
author Claus Gittinger <cg@exept.de>
Thu, 10 May 2018 02:06:08 +0200
changeset 1919 2a68b01c7f02
parent 1918 208bd3c139d3
child 1920 bb1f0cf27979
permissions -rw-r--r--
#DOCUMENTATION by cg class: RegressionTests::TimeDurationTest added: #test_00_Basic #test_05_Reading changed: #test_00_Reading

"{ 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 asMilliseconds == 1000).
    self assert:(t asMicroseconds = 1000000).
    self assert:(t asNanoseconds = 1000000000).
    self assert:(t asPicoseconds = 1000000000000).

    t := TimeDuration fromMilliseconds:345.
    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 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 asTruncatedMicroseconds = 0).
    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 asTruncatedMilliseconds == 0).
    self assert:(t asTruncatedMicroseconds = 0).
    self assert:(t asTruncatedNanoseconds = 0).
    self assert:(t asPicoseconds = 345).

    "
     self new test_00_Basic
    "
!

test_00_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:'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,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)).

    "
     self new test_00_Reading
    "
!

test_05_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:'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,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)).

    "
     self new test_05_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"
! !

!TimeDurationTest class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !