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