aboutsummaryrefslogtreecommitdiff
path: root/_experimental/client/charts/utils_test.go
blob: cd5a903695653a2ef421b6024a37af6c430eb875 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package charts

import "testing"

func TestRound(t *testing.T) {
	check := []struct {
		value    float64
		expected int
	}{
		{-1.1, -1},
		{-0.9, -1},
		{-0.5, -1},
		{-0.1, 0},
		{0.1, 0},
		{0.5, 0},
		{0.9, 1},
		{1.1, 1},
	}

	for _, v := range check {
		got := round(v.value)
		if got != v.expected {
			t.Errorf("round(%v) returned unexpected value. Expected: %v, Got: %v", v.value, v.expected, got)
		}
	}
}

func TestFlooredPOT(t *testing.T) {
	check := []struct {
		value    float64
		expected int
	}{
		{0.5, 1},
		{0, 1},
		{1, 1},
		{2, 2},
		{3, 2},
		{4, 4},
		{5, 4},
	}

	for _, v := range check {
		got := flooredPOT(v.value)
		if got != v.expected {
			t.Errorf("flooredPOT(%v) returned unexpected value. Expected: %v, Got: %v", v.value, v.expected, got)
		}
	}
}

func TestCalculateStepInterval(t *testing.T) {
	check := []struct {
		base, rng, cnt int
		expected       int
	}{
		//  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18
		//     2     4     6     8     10    12    14    16    18
		//           4           8           12          16
		//                       8                       16
		//                                               16
		{1, 18, 18, 1},
		{1, 18, 9, 2},
		{1, 18, 4, 4},
		{1, 18, 2, 8},
		{1, 18, 1, 16},

		//  3  6  9  12 15 18 21 24 27 30 33 36 39 42 45 48 51 54
		//     6     12    18    24    30    36    42    48    54
		//           12          24          36          48
		//                       24                      48
		//                                               48
		{3, 54, 18, 3},
		{3, 54, 9, 6},
		{3, 54, 4, 12},
		{3, 54, 2, 24},
		{3, 54, 1, 48},

		//  5  10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90
		//     10    20    30    40    50    60    70    80    90
		//           20          40          60          80
		//                       40                      80
		//                                               80
		{5, 90, 18, 5},
		{5, 90, 9, 10},
		{5, 90, 4, 20},
		{5, 90, 2, 40},
		{5, 90, 1, 80},
	}

	for _, v := range check {
		got := calculateStepInterval(v.base, v.rng, v.cnt)
		if got != v.expected {
			t.Errorf("calculateStepInterval(%v, %v, %v) returned unexpected value. Expected: %v, Got: %v", v.base, v.rng, v.cnt, v.expected, got)
		}
	}
}