New upstream version 0.0~git20160804.0.4c0e845
Martín Ferrari
7 years ago
0 | Copyright (C) 2013 Blake Mizerany | |
1 | ||
2 | Permission is hereby granted, free of charge, to any person obtaining | |
3 | a copy of this software and associated documentation files (the | |
4 | "Software"), to deal in the Software without restriction, including | |
5 | without limitation the rights to use, copy, modify, merge, publish, | |
6 | distribute, sublicense, and/or sell copies of the Software, and to | |
7 | permit persons to whom the Software is furnished to do so, subject to | |
8 | the following conditions: | |
9 | ||
10 | The above copyright notice and this permission notice shall be | |
11 | included in all copies or substantial portions of the Software. | |
12 | ||
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
14 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
15 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
16 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
17 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
18 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
19 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
132 | 132 | if l == 0 { |
133 | 133 | return 0 |
134 | 134 | } |
135 | i := int(float64(l) * q) | |
135 | i := int(math.Ceil(float64(l) * q)) | |
136 | 136 | if i > 0 { |
137 | 137 | i -= 1 |
138 | 138 | } |
32 | 32 | for quantile, epsilon := range Targets { |
33 | 33 | n := float64(len(a)) |
34 | 34 | k := int(quantile * n) |
35 | if k < 1 { | |
36 | k = 1 | |
37 | } | |
35 | 38 | lower := int((quantile - epsilon) * n) |
36 | 39 | if lower < 1 { |
37 | 40 | lower = 1 |
98 | 101 | verifyPercsWithAbsoluteEpsilon(t, a, s) |
99 | 102 | } |
100 | 103 | |
104 | func TestTargetedQuerySmallSampleSize(t *testing.T) { | |
105 | rand.Seed(42) | |
106 | s := NewTargeted(TargetsSmallEpsilon) | |
107 | a := []float64{1, 2, 3, 4, 5} | |
108 | for _, v := range a { | |
109 | s.Insert(v) | |
110 | } | |
111 | verifyPercsWithAbsoluteEpsilon(t, a, s) | |
112 | // If not yet flushed, results should be precise: | |
113 | if !s.flushed() { | |
114 | for φ, want := range map[float64]float64{ | |
115 | 0.01: 1, | |
116 | 0.10: 1, | |
117 | 0.50: 3, | |
118 | 0.90: 5, | |
119 | 0.99: 5, | |
120 | } { | |
121 | if got := s.Query(φ); got != want { | |
122 | t.Errorf("want %f for φ=%f, got %f", want, φ, got) | |
123 | } | |
124 | } | |
125 | } | |
126 | } | |
127 | ||
101 | 128 | func TestLowBiasedQuery(t *testing.T) { |
102 | 129 | rand.Seed(42) |
103 | 130 | s := NewLowBiased(RelativeEpsilon) |