comparison pkg/common/time_test.go @ 3332:c86a8e70b40f

Made time interpolation more precise and added a unit test.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 20 May 2019 16:36:19 +0200
parents
children ecb4baa2be1a
comparison
equal deleted inserted replaced
3331:6172acfa7ff5 3332:c86a8e70b40f
1 // This is Free Software under GNU Affero General Public License v >= 3.0
2 // without warranty, see README.md and license for details.
3 //
4 // SPDX-License-Identifier: AGPL-3.0-or-later
5 // License-Filename: LICENSES/AGPL-3.0.txt
6 //
7 // Copyright (C) 2018, 2019 by via donau
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
9 // Software engineering by Intevation GmbH
10 //
11 // Author(s):
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
13
14 package common
15
16 import (
17 "testing"
18 "time"
19 )
20
21 func TestInterpolateTimeByValue(t *testing.T) {
22
23 t1 := time.Now().UTC()
24 t2 := t1.Add(time.Hour).UTC()
25
26 f := InterpolateTimeByValue(t1, 10, t2, 20)
27
28 v1, _ := f(10)
29 v2, _ := f(20)
30 v3, _ := f(15)
31
32 t3 := t1.Add(time.Hour / 2)
33
34 d1 := v1.Sub(t1)
35 d2 := v2.Sub(t2)
36 d3 := v3.Sub(t3)
37
38 if d1 < 0 {
39 d1 = -d1
40 }
41
42 if d1 > 100*time.Microsecond {
43 t.Errorf("difference too big t1: %v\n", d1)
44 }
45
46 if d2 < 0 {
47 d2 = -d2
48 }
49
50 if d2 > 100*time.Microsecond {
51 t.Errorf("difference too big t2: %v\n", d2)
52 }
53
54 if d3 < 0 {
55 d3 = -d3
56 }
57
58 if d3 > 100*time.Microsecond {
59 t.Errorf("difference too big t3: %v\n", d3)
60 }
61 }