1plugin =
2{
3    type = "piglet",
4    name = "piglet::raw_buffer",
5    test = function()
6        dofile(SCRIPT_DIR .. "/../common.lua")
7        return run_tests(tests)
8    end
9}
10
11INIT_SIZE = 16
12INIT_STRING = "foobar"
13INIT_16_CONTENT = string.rep("00 ", 16)
14
15tests =
16{
17    initialize_default = function()
18        local rb = RawBuffer.new()
19        assert(rb)
20        assert(rb:size() == 0)
21    end,
22
23    initialize_with_size = function()
24        local rb = RawBuffer.new(INIT_SIZE)
25        assert(rb:size() == INIT_SIZE)
26        assert(rb:read() == INIT_16_CONTENT:encode_hex())
27    end,
28
29    initialize_with_string = function()
30        local rb = RawBuffer.new(INIT_STRING)
31        assert(rb:size() == string.len(INIT_STRING))
32        assert(rb:read() == INIT_STRING)
33
34        rb = RawBuffer.new(INIT_STRING, INIT_SIZE)
35        assert(rb:size() == INIT_SIZE)
36        assert(rb:read(0, #INIT_STRING) == INIT_STRING)
37        local rv = rb:read(#INIT_STRING, rb:size())
38        assert(rb:read(#INIT_STRING, rb:size()) == "\0\0\0\0\0\0\0\0\0\0")
39    end,
40
41    write = function()
42        local rb = RawBuffer.new()
43
44        -- write without offset
45        rb:write("foobar")
46        assert(rb:size() == 6, "write() extends length")
47        assert(rb:read() == "foobar")
48
49        -- write with offset
50        rb = RawBuffer.new()
51        -- zero offset (should be same as no offset)
52        rb:write("foobar", 0)
53        assert(rb:size() == 6, "size should be 6, not " .. rb:size())
54        assert(rb:read() == "foobar", "contents should be 'foobar', not '" .. rb:read() .. "'")
55
56        -- non-zero offset
57        rb = RawBuffer.new()
58        rb:write("foobar", 1)
59        assert(rb:size() == 7)
60        assert(rb:read() == "\0foobar")
61    end,
62
63    read_empty = function()
64        local rv
65        local rb = RawBuffer.new()
66
67        -- read with no args
68        rv = rb:read()
69        assert(#rv == 0, "length should equal 0, not " .. tostring(rv))
70
71        -- read with 1 arg
72        rv = rb:read(0)
73        assert(#rv == 0, "length should equal 0, not " .. tostring(rv))
74
75        -- read oor with 1 arg (-1, 10)
76        check.raises(function() rb:read(-1) end)
77        check.raises(function() rb:read(2) end)
78
79        -- read with 2 args
80        rv = rb:read(0, 0)
81        assert(#rv == 0, "length should equal 0, not " .. tostring(rv))
82
83        -- read oor with 2 args
84        check.raises(function() rb:read(-1, 0) end)
85        check.raises(function() rb:read(0, 2) end)
86    end,
87
88    read_nonempty = function()
89        local rb = RawBuffer.new("foobar")
90
91        -- read with no args
92        local rv = rb:read()
93        assert(rv == "foobar")
94
95        -- read with 1 arg (full string)
96        rv = rb:read(rb:size())
97        assert(rv == "foobar")
98
99        -- read with 1 arg (slice), length
100        rv = rb:read(2)
101        assert(rv == "fo")
102
103        -- read oob with 1 arg
104        check.raises(function() rb:read(10) end)
105
106        -- read with 2 args (full string), offset, length
107        rv = rb:read(0, rb:size())
108        assert(rv == "foobar")
109
110        -- read with 2 args (slice begin/end/middle)
111        rv = rb:read(0, rb:size() - 1)
112        assert(rv == "fooba")
113        rv = rb:read(1, rb:size())
114        assert(rv == "oobar")
115        rv = rb:read(1, rb:size() - 1)
116        assert(rv == "ooba")
117
118        -- read oob with 2 args (offset/length)
119        check.raises(function() rb:read(-1, rb:size()) end)
120        check.raises(function() rb:read(0, rb:size() + 1) end)
121    end,
122
123    resize = function()
124        local rb = RawBuffer.new()
125        -- resize
126        rb:resize(4)
127        assert(rb:size() == 4)
128        -- new contents is null-initialized
129        assert(rb:read() == "\0\0\0\0")
130        -- resize less
131        rb:resize(3)
132        assert(rb:size() == 3)
133        assert(rb:read() == "\0\0\0")
134    end
135}
136