]> Cypherpunks.ru repositories - gotai64n.git/blob - tai64n_test.go
Unify copyright comment format
[gotai64n.git] / tai64n_test.go
1 /*
2 go.cypherpunks.ru/tai64n -- Pure Go TAI64/TAI64N implementation
3 Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package tai64n
19
20 import (
21         "testing"
22         "time"
23 )
24
25 func TestVector(t *testing.T) {
26         tm, err := Decode("400000002a2b2c2d")
27         if err != nil {
28                 t.Fatal(err)
29         }
30
31         ref := time.Date(1992, 6, 2, 8, 7, 9, 0, time.UTC).Add(-Leapsecs1972 * time.Second)
32         if !tm.Equal(ref) {
33                 t.Fatal("TAI64 != reference")
34         }
35
36         tm = LeapsecsSub(tm)
37         ref = time.Date(1992, 6, 2, 8, 6, 43, 0, time.UTC)
38         if !tm.Equal(ref) {
39                 t.Fatal("UTC != reference")
40         }
41 }
42
43 func BenchmarkTAI64(b *testing.B) {
44         now := time.Now()
45         now = time.Unix(now.Unix(), 0)
46         tai := new(TAI64)
47         b.ResetTimer()
48         for i := 0; i < b.N; i++ {
49                 tai.FromTime(now)
50                 if !ToTime(tai[:]).Equal(now) {
51                         b.FailNow()
52                 }
53         }
54 }
55
56 func BenchmarkTAI64N(b *testing.B) {
57         now := time.Now()
58         now = time.Unix(now.Unix(), now.UnixNano())
59         tai := new(TAI64N)
60         b.ResetTimer()
61         for i := 0; i < b.N; i++ {
62                 tai.FromTime(now)
63                 if !ToTime(tai[:]).Equal(now) {
64                         b.FailNow()
65                 }
66         }
67 }