From 09216f63ba29b777beda6d4691eb8067af0af5e2 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Fri, 25 Dec 2020 15:22:37 +0900 Subject: test/test_trick.rb: Add a test file for TRICK entries (#3988) * test/test_trick.rb: Add a test file for TRICK entries Co-authored-by: Nobuyoshi Nakada --- test/test_trick.rb | 191 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 test/test_trick.rb (limited to 'test/test_trick.rb') diff --git a/test/test_trick.rb b/test/test_trick.rb new file mode 100644 index 0000000000..c61a53f7b6 --- /dev/null +++ b/test/test_trick.rb @@ -0,0 +1,191 @@ +require "test/unit" +require "ripper" +require "envutil" + +# This is a test suite for TRICK entries, joke Ruby program contest. +# The programs are very unusual, and not practical. +# Feel free to comment them out if they bother you. +# I'll appreciate it if you could notify mame + +class TestTRICK2013 < Test::Unit::TestCase + def test_kinaba + src = File.join(__dir__, "../sample/trick2013/kinaba/entry.rb") + expected = [*" ".."~"].join("") # all ASCII printables + assert_in_out_err(["-W0", src], "", [expected]) + assert_equal(expected, File.read(src).chomp.chars.sort.join) + end + + def test_mame + src = File.join(__dir__, "../sample/trick2013/mame/entry.rb") + ignore_dsp = "def open(_file, _mode); s = ''; def s.flush; self;end; yield s; end;" + assert_in_out_err(["-W0"], ignore_dsp + File.read(src), File.read(src).lines(chomp: true)) + end + + def test_shinh + src = File.join(__dir__, "../sample/trick2013/shinh/entry.rb") + assert_in_out_err(["-W0", src], "", []) + end + + def test_yhara + src = File.join(__dir__, "../sample/trick2013/yhara/entry.rb") + assert_in_out_err(["-W0", src], "", ["JUST ANOTHER RUBY HACKER"]) + end +end + +class TestTRICK2015 < Test::Unit::TestCase + def test_kinaba + src = File.join(__dir__, "../sample/trick2015/kinaba/entry.rb") + + # calculate the first 10000 digits of Pi + n = 10000 + a = b = 10 ** n + (n * 8 + 1).step(3, -2) do |i| + a = (i / 2) * (a + b * 2) / i + end + pi = "3#{ a - b }" + + assert_in_out_err(["-W0", src], "", [pi]) + assert_equal(pi[0, 242], Ripper.tokenize(File.read(src)).grep(/\S/).map{|t|t.size%10}.join) + end + + def test_ksk_1 + src = File.join(__dir__, "../sample/trick2015/ksk_1/entry.rb") + + # calculate Collatz sequence + s = ["27"] + n = 27 + until n == 1 + n = n.even? ? n / 2 : n * 3 + 1 + s << n.to_s + end + + assert_in_out_err(["-W0", src, "27"], "", s) + end + + def test_monae + src = File.join(__dir__, "../sample/trick2015/monae/entry.rb") + + code = File.read(src) + expected = code.lines(chomp: true) + (0..15).map { "" } + code.lines.each_with_index do |s, y| + y += 16 + s.chomp.chars.each_with_index do |c, x| + x += 16 + expected[y] << " " while expected[y].size < x + expected[y][x] = c if c != " " + end + end + expected = /\A#{ expected.map {|s| "#{ Regexp.quote(s) }\s*\n" }.join }\z/ + + assert_in_out_err(["-W0", src], "", expected) + end + + def test_eregon + src = File.join(__dir__, "../sample/trick2015/eregon/entry.rb") + + assert_in_out_err(["-W0", src], "", <