-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_stack.t
130 lines (115 loc) · 2.97 KB
/
test_stack.t
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
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
-- SPDX-FileContributor: René Hiemstra <[email protected]>
--
-- SPDX-License-Identifier: MIT
local alloc = require('alloc')
local stack = require("stack")
import "terratest/terratest"
testenv "DynamicStack" do
local T = double
local stack = stack.DynamicStack(T)
local DefaultAllocator = alloc.DefaultAllocator()
terracode
var alloc: DefaultAllocator
var s = stack.new(&alloc, 3)
end
testset "new" do
test s:size() == 0
test s:capacity() == 3
test [stack.traits.eltype == T]
test s.data:owns_resource()
end
testset "push" do
terracode
s:push(1.0)
s:push(2.0)
end
test s:size() == 2
test s:capacity() == 3
end
testset "pop" do
terracode
s:push(1.0)
s:push(2.0)
var x = s:pop()
end
test s:size() == 1
test s:capacity() == 3
test x == 2.0
end
testset "apply, set, get" do
terracode
s:push(1.0)
s:push(2.0)
s:push(3.0)
end
test s(0) == 1.0
test s(1) == 2.0
test s:get(2) == 3.0
terracode
s(0) = 3.0
s(1) = 4.0
s:set(2, 5.0)
end
test s(0) == 3.0
test s(1) == 4.0
test s:get(2) == 5.0
test s:size() == 3
test s:capacity() == 3
end
testset "insert" do
terracode
s:insert(0, 1.0)
s:push(2.0)
s:push(3.0)
s:insert(1, 4.0)
s:insert(3, -2.0)
end
test s(0) == 1.0
test s(1) == 4.0
test s:get(2) == 2.0
test s:get(3) == -2.0
test s:get(4) == 3.0
test s:size() == 5
end
testset "reallocate" do
terracode
s:push(1.0)
s:push(2.0)
s:push(3.0)
s:push(4.0) --triggering reallocate (new capacity is twice old capacity)
s:push(5.0)
end
test s:size() == 5
test s:capacity() == 7
test s(0) == 1
test s(1) == 2
test s(2) == 3
test s(3) == 4
test s(4) == 5
end
testset "__copy" do
terracode
s:push(1.0)
s:push(2.0)
var x = s
end
test s.data:isempty()
test x.data:owns_resource()
test x:size() == 2 and x:capacity() == 3
test x(0) == 1.0 and x(1) == 2.0
end
local smrtblock = alloc.SmartBlock(T)
testset "__dtor" do
terracode
var p : &smrtblock
do
var v = stack.new(&alloc, 4)
--the following raises an issue in terralib
p = &v.data
end --v:__dtor() is called here by the compiler
end
test p:isempty()
end
end