~ubuntu-branches/ubuntu/wily/ruby-ferret/wily

« back to all changes in this revision

Viewing changes to test/unit/utils/tc_number_tools.rb

  • Committer: Bazaar Package Importer
  • Author(s): Antonio Terceiro
  • Date: 2011-07-28 00:02:49 UTC
  • Revision ID: james.westby@ubuntu.com-20110728000249-v0443y69ftcpxwi6
Tags: upstream-0.11.6
ImportĀ upstreamĀ versionĀ 0.11.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require File.dirname(__FILE__) + "/../../test_helper"
 
2
require 'ferret/number_tools'
 
3
 
 
4
 
 
5
class NumberToolsTest < Test::Unit::TestCase
 
6
  include Ferret::Utils
 
7
 
 
8
  def test_to_i_lex_near_zero()
 
9
    (-10..10).each do |num|
 
10
      assert(num.to_s_lex > (num-1).to_s_lex,
 
11
             "Strings should sort correctly but " +
 
12
             "#{num.to_s_lex} <= #{(num-1).to_s_lex}")
 
13
      assert_equal(num, num.to_s_lex.to_i_lex)
 
14
    end
 
15
  end
 
16
 
 
17
  def test_to_i_pad_near_zero()
 
18
    (1..10).each do |num|
 
19
      assert(num.to_s_pad(3) > (num-1).to_s_pad(3),
 
20
             "Strings should sort correctly but " +
 
21
             "#{num.to_s_pad(3)} <= #{(num-1).to_s_pad(3)}")
 
22
      assert_equal(num, num.to_s_pad(3).to_i)
 
23
    end
 
24
  end
 
25
 
 
26
  def test_to_i_lex_larger_numbers
 
27
    100.times do
 
28
      num1 = rand(10000000000000000000000000000000000)
 
29
      num2 = rand(10000000000000000000000000000000000)
 
30
      num1 *= -1 if rand(2) == 0
 
31
      num2 *= -1 if rand(2) == 0
 
32
 
 
33
      assert_equal(num1, num1.to_s_lex.to_i_lex)
 
34
      assert_equal(num2, num2.to_s_lex.to_i_lex)
 
35
      assert_equal(num1 < num2, num1.to_s_lex < num2.to_s_lex, 
 
36
                   "Strings should sort correctly but " +
 
37
                   "#{num1} < #{num2} == #{num1 < num2} but " +
 
38
                   "#{num1.to_s_lex} < #{num2.to_s_lex} == " +
 
39
                   "#{num1.to_s_lex < num2.to_s_lex}")
 
40
    end
 
41
  end
 
42
 
 
43
  def test_to_i_pad
 
44
    100.times do
 
45
      num1 = rand(10000000000000000000000000000000000)
 
46
      num2 = rand(10000000000000000000000000000000000)
 
47
      assert_equal(num1, num1.to_s_pad(35).to_i)
 
48
      assert_equal(num2, num2.to_s_pad(35).to_i)
 
49
      assert_equal(num1 < num2, num1.to_s_pad(35) < num2.to_s_pad(35), 
 
50
                   "Strings should sort correctly but " +
 
51
                   "#{num1} < #{num2} == #{num1 < num2} but " +
 
52
                   "#{num1.to_s_pad(35)} < #{num2.to_s_pad(35)} == " +
 
53
                   "#{num1.to_s_pad(35) < num2.to_s_pad(35)}")
 
54
    end
 
55
  end
 
56
  
 
57
  def test_time_to_s_lex
 
58
    t_num = Time.now.to_i - 365*24*60*60 # prevent range error
 
59
 
 
60
    10.times do
 
61
      t1 = Time.now - rand(t_num)
 
62
      t2 = Time.now - rand(t_num)
 
63
      assert_equal(t1.to_s, t1.to_s_lex(:second).to_time_lex.to_s)
 
64
      assert_equal(t2.to_s, t2.to_s_lex(:second).to_time_lex.to_s)
 
65
      [:year, :month, :day, :hour, :minute, :second, :millisecond].each do |prec|
 
66
        t1_x = t1.to_s_lex(prec).to_time_lex
 
67
        t2_x = t2.to_s_lex(prec).to_time_lex
 
68
        assert_equal(t1_x < t2_x, t1.to_s_lex(prec) < t2.to_s_lex(prec), 
 
69
                     "Strings should sort correctly but " +
 
70
                     "#{t1_x} < #{t2_x} == #{t1_x < t2_x} but " +
 
71
                     "#{t1.to_s_lex(prec)} < #{t2.to_s_lex(prec)} == " +
 
72
                     "#{t1.to_s_lex(prec) < t2.to_s_lex(prec)}")
 
73
      end
 
74
    end
 
75
  end
 
76
 
 
77
  def test_date_to_s_lex
 
78
    10.times do
 
79
      d1 = Date.civil(rand(2200), rand(12) + 1, rand(28) + 1)
 
80
      d2 = Date.civil(rand(2200), rand(12) + 1, rand(28) + 1)
 
81
      assert_equal(d1.to_s, d1.to_s_lex(:day).to_date_lex.to_s)
 
82
      assert_equal(d2.to_s, d2.to_s_lex(:day).to_date_lex.to_s)
 
83
      [:year, :month, :day].each do |prec|
 
84
        d1_x = d1.to_s_lex(prec).to_date_lex
 
85
        d2_x = d2.to_s_lex(prec).to_date_lex
 
86
        assert_equal(d1_x < d2_x, d1.to_s_lex(prec) < d2.to_s_lex(prec), 
 
87
                     "Strings should sort correctly but " +
 
88
                     "#{d1_x} < #{d2_x} == #{d1_x < d2_x} but " +
 
89
                     "#{d1.to_s_lex(prec)} < #{d2.to_s_lex(prec)} == " +
 
90
                     "#{d1.to_s_lex(prec) < d2.to_s_lex(prec)}")
 
91
      end
 
92
 
 
93
    end
 
94
  end
 
95
 
 
96
  def test_date_time_to_s_lex
 
97
    10.times do
 
98
      d1 = "#{rand(600) + 1600}-#{rand(12)+1}-#{rand(28)+1} " +
 
99
           "#{rand(24)}:#{rand(60)}:#{rand(60)}"
 
100
      d2 = "#{rand(600) + 1600}-#{rand(12)+1}-#{rand(28)+1} " +
 
101
           "#{rand(24)}:#{rand(60)}:#{rand(60)}"
 
102
      d1 = DateTime.strptime(d1, "%Y-%m-%d %H:%M:%S")
 
103
      d2 = DateTime.strptime(d2, "%Y-%m-%d %H:%M:%S")
 
104
      assert_equal(d1.to_s, d1.to_s_lex(:second).to_date_time_lex.to_s)
 
105
      assert_equal(d2.to_s, d2.to_s_lex(:second).to_date_time_lex.to_s)
 
106
      [:year, :month, :day, :hour, :minute, :second].each do |prec|
 
107
        d1_x = d1.to_s_lex(prec).to_date_lex
 
108
        d2_x = d2.to_s_lex(prec).to_date_lex
 
109
        assert_equal(d1_x < d2_x, d1.to_s_lex(prec) < d2.to_s_lex(prec), 
 
110
                     "Strings should sort correctly but " +
 
111
                     "#{d1_x} < #{d2_x} == #{d1_x < d2_x} but " +
 
112
                     "#{d1.to_s_lex(prec)} < #{d2.to_s_lex(prec)} == " +
 
113
                     "#{d1.to_s_lex(prec) < d2.to_s_lex(prec)}")
 
114
      end
 
115
    end
 
116
  end
 
117
end