cast 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,174 +0,0 @@
1
- $: << File.expand_path('../lib')
2
- require 'cast'
3
- require 'test/unit'
4
-
5
- if true
6
- class C::Node
7
- def pretty_print q
8
- q.text self.to_debug
9
- end
10
- end
11
- end
12
-
13
- class Array
14
- def same_list? other
15
- self.length == other.length or
16
- return false
17
- self.zip(other).all? do |mine, yours|
18
- mine.equal? yours or
19
- return false
20
- end
21
- end
22
- end
23
-
24
- class Integer
25
- ###
26
- ### Return a `self'-element array containing the result of the given
27
- ### block.
28
- ###
29
- def of &blk
30
- Array.new(self, &blk)
31
- end
32
- end
33
-
34
- module Test::Unit::Assertions
35
- ###
36
- ### Assert that the given ast's nodes' parents are correct, and
37
- ### there aren't non-Nodes where there shouldn't be.
38
- ###
39
- def assert_tree ast
40
- meth = 'unknown method'
41
- caller.each do |line|
42
- if line =~ /in `(test_.*?)'/ #`
43
- meth = $1
44
- break
45
- end
46
- end
47
- filename = "#{self.class}_#{meth}.out"
48
- begin
49
- assert_tree1(ast, nil)
50
- assert(true)
51
- rescue BadTreeError => e
52
- require 'pp'
53
- open("#{filename}", 'w'){|f| PP.pp(ast, f)}
54
- flunk("#{e.message}. Output dumped to `#{filename}'.")
55
- end
56
- end
57
- ###
58
- def assert_tree1 x, parent
59
- if x.is_a? C::Node
60
- parent.equal? x.parent or
61
- raise BadTreeError, "#{x.class}:0x#{(x.id << 1).to_s(16)} has #{x.parent ? 'wrong' : 'no'} parent"
62
- x.fields.each do |field|
63
- next if !field.child?
64
- val = x.send(field.reader)
65
- next if val.nil?
66
- val.is_a? C::Node or
67
- raise BadTreeError, "#{x.class}:0x#{(x.id << 1).to_s(16)} is a non-Node child"
68
- assert_tree1(val, x)
69
- end
70
- end
71
- end
72
- class BadTreeError < StandardError; end
73
- ###
74
- ### Assert that `arg' is a C::NodeList.
75
- ###
76
- def assert_list arg
77
- assert_kind_of(C::NodeList, arg)
78
- end
79
- ###
80
- ### Assert that `arg' is an empty C::NodeList.
81
- ###
82
- def assert_empty_list arg
83
- assert_list arg
84
- assert(arg.empty?)
85
- end
86
- ###
87
- ### Assert that the elements of exp are the same as those of out,
88
- ### and are in the same order.
89
- ###
90
- def assert_same_list exp, out
91
- assert_equal(exp.length, out.length, "Checking length")
92
- (0...exp.length).each do |i|
93
- assert_same(exp[i], out[i], "At index #{i} (of 0...#{exp.length})")
94
- end
95
- end
96
- ###
97
- ### Assert that out is ==, but not the same as exp (i.e., it is a
98
- ### copy).
99
- ###
100
- def assert_copy exp, out
101
- assert_not_same exp, out
102
- assert_equal exp, out
103
- end
104
- ###
105
- ### Assert the invariants of `node'.
106
- ###
107
- def assert_invariants node
108
- node.assert_invariants(self)
109
- end
110
- ###
111
- ### Return a not-too-trivial C program string.
112
- ###
113
- def prog
114
- return <<EOS
115
- int main(int argc, char **argv) {
116
- struct S {
117
- int i, j;
118
- float f, g;
119
- } x;
120
- x.i = (int)argv[2][5];
121
- return 0;
122
- }
123
- EOS
124
- end
125
- end
126
-
127
- module CheckAst
128
- INDENT = ' '
129
-
130
- ParseError = C::ParseError
131
-
132
- def check_ast test_data
133
- inp, exp = test_data.split(/^----+\n/)
134
- ast = yield(inp)
135
- assert_tree(ast)
136
- assert ast.is_a?(C::Node)
137
- out = ast.to_debug
138
- assert_equal_debug_strs(exp, out)
139
- end
140
-
141
- def assert_equal_debug_strs exp, out
142
- ## remove EOL space
143
- out = out.gsub(/ *$/, '')
144
- exp = exp.gsub(/ *$/, '')
145
-
146
- ## normalize BOL space
147
- exp.gsub!(%r'^#{INDENT}*') do |s|
148
- levels = s.length / INDENT.length
149
- C::Node::TO_DEBUG_TAB*levels
150
- end
151
-
152
- ## compare
153
- meth = 'unknown method'
154
- caller.each do |line|
155
- if line =~ /in `(test_.*?)'/ #`
156
- meth = $1
157
- break
158
- end
159
- end
160
-
161
- filename_prefix = "#{self.class}_#{meth}"
162
-
163
- assert_block("Debug strings unequal. Output dumped to #{filename_prefix}.{exp,out}") do
164
- if out == exp
165
- true
166
- else
167
- classname = self.class.name.split(/::/)[-1]
168
- open("#{filename_prefix}.out", 'w'){|f| f.print(out)}
169
- open("#{filename_prefix}.exp", 'w'){|f| f.print(exp)}
170
- false
171
- end
172
- end
173
- end
174
- end