summaryrefslogtreecommitdiff
path: root/test/test_trick.rb
diff options
context:
space:
mode:
authorYusuke Endoh <mame@ruby-lang.org>2023-02-26 00:06:24 +0900
committerYusuke Endoh <mame@ruby-lang.org>2023-02-27 11:20:42 +0900
commitb6704201a3ef6a7f07da8d4ca82499710a3eea10 (patch)
treea674b31db7c5ed73aa2172690bba4c0a7f3a3040 /test/test_trick.rb
parent2535b1819f910f0e12bf609c72e45f7230a060c9 (diff)
downloadruby-b6704201a3ef6a7f07da8d4ca82499710a3eea10.tar.gz
Add all-ruby-quine as a sample code
This sample code works on all release versions of Ruby, from Ruby 0.49.
Diffstat (limited to 'test/test_trick.rb')
-rw-r--r--test/test_trick.rb32
1 files changed, 32 insertions, 0 deletions
diff --git a/test/test_trick.rb b/test/test_trick.rb
index 763f493899..201dd50b9b 100644
--- a/test/test_trick.rb
+++ b/test/test_trick.rb
@@ -1,6 +1,7 @@
require "test/unit"
require "ripper"
require "envutil"
+require "stringio"
# This is a test suite for TRICK entries, joke Ruby program contest.
# The programs are very unusual, and not practical.
@@ -212,3 +213,34 @@ class TestTRICK2022 < Test::Unit::TestCase
assert_in_out_err(["-W0", "-c", src], "", ["Syntax OK"])
end
end
+
+# https://github.com/mame/all-ruby-quine
+class TestAllRubyQuine < Test::Unit::TestCase
+ def test_all_ruby_quine
+ stdout_bak = $stdout
+ $stdout = StringIO.new
+ src = File.read(File.join(__dir__, "../sample/all-ruby-quine.rb"))
+
+ eval(src)
+
+ out = $stdout.string.lines(chomp: true)
+ $stdout = stdout_bak
+
+ # cheat OCR
+ font = {
+ "-" => 0x7ffffffffffe03fffffffffff, "." => 0x7fffffffffffffffffffc7f8f, "_" => 0x7fffffffffffffffffffff800,
+ "0" => 0x6030e03e07c0f81f03e038603, "1" => 0x70fc1f23fc7f8ff1fe3fc7c01, "2" => 0x4011f1fe3fc7e1f0f87c3f800,
+ "3" => 0x4031e3fe3f8e03fe3fe078c03, "4" => 0x783e0788e318e31c6003f1fe3, "5" => 0x0001fe3fc7f801fe1fe078401,
+ "6" => 0x78083e3fc7f8011e03e038401, "7" => 0x000fe1fc3f0fc3f0fc3f0fc3f, "8" => 0x4011f03e238e038e23e07c401,
+ "9" => 0x4010e03e03c400ff1fe078401, "a" => 0x7fffff00c787f88003e078408, "b" => 0x0ff1fe3fc408701f03e078001,
+ "c" => 0x7fffff8063c0ff1fe3fe3c601, "d" => 0x7f8ff1fe3004781f03e038408,
+ }.invert
+ out = (0...out.first.size / 15).map do |i|
+ font[(3..11).map {|j| out[j][i * 15 + 5, 11] }.join.gsub(/\S/, "#").tr("# ", "10").to_i(2)]
+ end.join
+
+ assert_equal(RUBY_VERSION, out)
+ ensure
+ $stdout = stdout_bak
+ end
+end