$: << File.expand_path('../lib') require 'cast' require 'test/unit' if true class C::Node def pretty_print q q.text self.to_debug end end end class Array def same_list? other self.length == other.length or return false self.zip(other).all? do |mine, yours| mine.equal? yours or return false end end end class Integer ### ### Return a `self'-element array containing the result of the given ### block. ### def of &blk Array.new(self, &blk) end end module Test::Unit::Assertions ### ### Assert that the given ast's nodes' parents are correct, and ### there aren't non-Nodes where there shouldn't be. ### def assert_tree ast meth = 'unknown method' caller.each do |line| if line =~ /in `(test_.*?)'/ #` meth = $1 break end end filename = "#{self.class}_#{meth}.out" begin assert_tree1(ast, nil) assert(true) rescue BadTreeError => e require 'pp' open("#{filename}", 'w'){|f| PP.pp(ast, f)} flunk("#{e.message}. Output dumped to `#{filename}'.") end end ### def assert_tree1 x, parent if x.is_a? C::Node parent.equal? x.parent or raise BadTreeError, "#{x.class}:0x#{(x.id << 1).to_s(16)} has #{x.parent ? 'wrong' : 'no'} parent" x.fields.each do |field| next if !field.child? val = x.send(field.reader) next if val.nil? val.is_a? C::Node or raise BadTreeError, "#{x.class}:0x#{(x.id << 1).to_s(16)} is a non-Node child" assert_tree1(val, x) end end end class BadTreeError < StandardError; end ### ### Assert that `arg' is a C::NodeList. ### def assert_list arg assert_kind_of(C::NodeList, arg) end ### ### Assert that `arg' is an empty C::NodeList. ### def assert_empty_list arg assert_list arg assert(arg.empty?) end ### ### Assert that the elements of exp are the same as those of out, ### and are in the same order. ### def assert_same_list exp, out assert_equal(exp.length, out.length, "Checking length") (0...exp.length).each do |i| assert_same(exp[i], out[i], "At index #{i} (of 0...#{exp.length})") end end ### ### Assert that out is ==, but not the same as exp (i.e., it is a ### copy). ### def assert_copy exp, out assert_not_same exp, out assert_equal exp, out end ### ### Assert the invariants of `node'. ### def assert_invariants node node.assert_invariants(self) end ### ### Return a not-too-trivial C program string. ### def prog return <