File: ts_aggregate.rb

package info (click to toggle)
ruby-aggregate 0.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 136 kB
  • sloc: ruby: 275; makefile: 2
file content (162 lines) | stat: -rw-r--r-- 3,735 bytes parent folder | download
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
require 'minitest/autorun'
require 'aggregate'

class SimpleStatsTest < Minitest::Test

  def setup
    @stats = Aggregate.new

    @@DATA.each do |x|
      @stats << x
    end
  end

  def test_stats_count
    assert_equal @@DATA.length, @stats.count
  end

  def test_stats_min_max
    sorted_data = @@DATA.sort

    assert_equal sorted_data[0], @stats.min
    assert_equal sorted_data.last, @stats.max
  end

  def test_stats_mean
    sum = 0
    @@DATA.each do |x|
      sum += x
    end

    assert_equal sum.to_f/@@DATA.length.to_f, @stats.mean
  end

  def test_bucket_counts

    #Test each iterator
    total_bucket_sum = 0
    i = 0
    @stats.each do |bucket, count|
      assert_equal 2**i, bucket

      total_bucket_sum += count
      i += 1
    end

    assert_equal total_bucket_sum, @@DATA.length

    #Test each_nonzero iterator
    prev_bucket = 0
    total_bucket_sum = 0
    @stats.each_nonzero do |bucket, count|
      assert bucket > prev_bucket
      refute_equal count, 0

      total_bucket_sum += count
    end

    assert_equal total_bucket_sum, @@DATA.length
  end

=begin
  def test_addition
    stats1 = Aggregate.new
    stats2 = Aggregate.new

    stats1 << 1
    stats2 << 3

    stats_sum = stats1 + stats2

    assert_equal stats_sum.count, stats1.count + stats2.count
  end
=end

  #XXX: Update test_bucket_contents() if you muck with @@DATA
  @@DATA = [ 1, 5, 4, 6, 1028, 1972, 16384, 16385, 16383]
  def test_bucket_contents
    #XXX: This is the only test so far that cares about the actual contents
    # of @@DATA, so if you update that array ... update this method too
    expected_buckets  = [1, 4, 1024, 8192, 16384]
    expected_counts =   [1, 3,    2,    1,     2]

    i = 0
    @stats.each_nonzero do |bucket, count|
      assert_equal expected_buckets[i], bucket
      assert_equal expected_counts[i],  count
      # Increment for the next test
      i += 1
    end
  end

  def test_histogram
    puts @stats.to_s
  end

  def test_outlier
    assert_equal 0, @stats.outliers_low
    assert_equal 0, @stats.outliers_high

    @stats << -1
    @stats << -2
    @stats << 0

    @stats << 2**128

    # This should be the last value in the last bucket, but Ruby's native
    # floats are not precise enough. Somewhere past 2^32 the log(x)/log(2)
    # breaks down. So it shows up as 128 (outlier) instead of 127
    #@stats << (2**128) - 1

    assert_equal 3, @stats.outliers_low
    assert_equal 1, @stats.outliers_high
  end

  def test_std_dev
    @stats.std_dev
  end
end

class LinearHistogramTest < Minitest::Test
  def setup
    @stats = Aggregate.new(0, 32768, 1024)

    @@DATA.each do |x|
      @stats << x
    end
  end

  def test_validation

    # Range cannot be 0
    assert_raises(ArgumentError) { Aggregate.new(32,32,4) }

    # Range cannot be negative
    assert_raises(ArgumentError) { Aggregate.new(32,16,4) }

    # Range cannot be < single bucket
    assert_raises(ArgumentError) { Aggregate.new(16,32,17) }

    # Range % width must equal 0 (for now)
    assert_raises(ArgumentError) { Aggregate.new(1,16384,1024) }
  end

  #XXX: Update test_bucket_contents() if you muck with @@DATA
  # 32768 is an outlier
  @@DATA = [ 0, 1, 5, 4, 6, 1028, 1972, 16384, 16385, 16383, 32768]
  def test_bucket_contents
    #XXX: This is the only test so far that cares about the actual contents
    # of @@DATA, so if you update that array ... update this method too
    expected_buckets  = [0, 1024,  15360, 16384]
    expected_counts =   [5, 2,     1,     2]

    i = 0
    @stats.each_nonzero do |bucket, count|
      assert_equal expected_buckets[i], bucket
      assert_equal expected_counts[i],  count
      # Increment for the next test
      i += 1
    end
  end

end