Skip to content

Instantly share code, notes, and snippets.

@jfirebaugh
Created January 12, 2012 22:47
Show Gist options
  • Select an option

  • Save jfirebaugh/1603645 to your computer and use it in GitHub Desktop.

Select an option

Save jfirebaugh/1603645 to your computer and use it in GitHub Desktop.
Encoding.list.each do |encoding|
print "Testing #{encoding.name}"
(0..0xffff).each do |i|
print "." if i % 256 == 0
begin
result = i.chr(encoding)
rescue RangeError
next
rescue Exception => e
puts "expected #{i}.chr(#{encoding.name.inspect}) to raise RangeError, but it raised #{e.inspect} instead"
next
end
if result.length != 1
puts "expected #{i}.chr(#{encoding.name.inspect}) to be a single character, but it was #{result.length}"
elsif !result.valid_encoding?
puts "expected #{i}.chr(#{encoding.name.inspect}) to return a validly encoded string, but it returned #{result.inspect} instead"
elsif result.encoding != encoding
puts "expected #{i}.chr(#{encoding.name.inspect}) to be encoded as #{encoding.name}"
elsif result.codepoints.to_a != [i]
puts "expected #{i}.chr(#{encoding.name.inspect}) to encode the codepoint #{i}, but it encoded #{result.codepoints.to_a} instead"
end
end
print "\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment