017cdf63c3e4f9e1eef0d575acc254002e98d6c0
[ruby-eet.git] / test / test_basic.rb
1 # $Id: test_basic.rb 59 2005-06-08 16:13:15Z tilman $
2
3 require "eet"
4 require "test/unit"
5 require "common"
6
7 class BasicTestData
8         def initialize
9                 @name = "moo"
10                 @short = 512
11                 @int = 1024
12                 @long_long = (2 << 63) - 1
13                 @flag = true
14                 @float = 1234.12424213138
15         end
16
17         private
18         def to_eet_name
19                 "BasicTest"
20         end
21
22         def to_eet_properties
23                 {"name" => [@name],
24                  "i16" => [@short, :short],
25                  "i32" => [@int],
26                  "i64" => [@long_long, :long_long],
27                  "flag" => [@flag],
28                  "f32" => [@float],
29                  "f64" => [@float, :double]}
30         end
31 end
32
33 class BasicTest < Test::Unit::TestCase
34         def test_basic
35                 data = BasicTestData.new.to_eet
36                 assert_not_nil(data)
37
38                 stream = nil
39
40                 assert_nothing_raised do
41                         stream = Eet::Stream.deserialize(data)
42                 end
43
44                 assert_equal(1, stream.length)
45                 assert_equal("BasicTest", stream.first.tag)
46
47                 assert_nothing_raised do
48                         stream = Eet::Stream.deserialize(stream.first.data)
49                 end
50
51                 assert_equal(7, stream.length)
52
53                 values = {"name" => "moo\0",
54                           "i16" => "\0\2", "i32" => "\0\4\0\0",
55                           "i64" => "\377" * 8, "flag" => "\1",
56                           "f32" => "0x1.3487f4p+10\0",
57                           "f64" => "0x1.3487f39544c4p+10\0"}
58                 values.each do |k, v|
59                         found = stream.find { |c| c.tag == k }
60                         assert_not_nil(found, "chunk not found - #{k}")
61                         assert_equal(v, found.data)
62                         stream.delete(found)
63                 end
64
65                 assert_equal([], stream)
66         end
67 end