PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/test/scanf/test_scanf.rb

http://github.com/ruby/ruby
Ruby | 304 lines | 255 code | 24 blank | 25 comment | 0 complexity | 02457f104ad35c6761a799f287a1707f MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # frozen_string_literal: false
  2. # $Id$
  3. #
  4. # scanf for Ruby
  5. #
  6. # Unit tests
  7. #
  8. require 'scanf.rb'
  9. require 'test/unit'
  10. require 'tempfile'
  11. # Comment out either of these lines to skip those tests.
  12. class TestStringScanf < Test::Unit::TestCase;end
  13. class TestIOScanf < Test::Unit::TestCase;end
  14. module ScanfTests
  15. def tests
  16. [
  17. # Scratchpad
  18. [ "%2[a]", "nbc", []],
  19. [ "%*d %*3d %*s", "123 +456 abc", [] ],
  20. [ "%d%c", "123 x", [ 123, " " ] ],
  21. [ "%d%c", "123x", [ 123, "x" ] ],
  22. [ "%d %c", "123x", [ 123, "x" ] ],
  23. [ "%d %c", "123 x", [ 123, "x" ] ],
  24. # Testing failures
  25. [ "%x", "x", [] ],
  26. [ "%2x", "x", [] ],
  27. [ "%i", "x", [] ],
  28. # ]; end; def nothing; [
  29. [ "%2i", "x", [] ],
  30. [ "%2o", "x", [] ],
  31. [ "%d", "x", [] ],
  32. [ "%2d", "x", [] ],
  33. [ "%3d", "+x3", [] ],
  34. [ "%d%[abc]", "eabc", [] ],
  35. [ "%d\n%[abc]", "\neabc", [] ],
  36. [ "%d%[^abc]", "ghiabc", [ ] ],
  37. [ "%d%[abc]", "abc", [] ],
  38. [ "%d%s", "", [] ],
  39. [ "%d%s", "blah 123 string", [] ],
  40. [ "%[\n]", "abc\n", [] ],
  41. [ "%f", "x", [] ],
  42. [ "%f", "z", [] ],
  43. [ "%f", "z3.2534", [] ],
  44. [ "", "", [] ],
  45. [ "", "abc 123", [] ],
  46. [ '%[^\\w]%c', "a...1", [] ],
  47. # Testing 'x'
  48. [ "%3x", "0xz", [0] ],
  49. # Testing 'i'
  50. [ "%3i", "097", [0] ],
  51. [ "%3i", "0xz", [0] ],
  52. [ "%1i", "3", [ 3 ] ],
  53. [ "%2i", "07", [ 7 ] ],
  54. [ "%2i", "0a", [ 0 ] ],
  55. # Testing 'c'
  56. [ "%3c", "abc", [ "abc" ] ],
  57. [ "%3c", "a\nb", [ "a\nb" ] ],
  58. [ "%3c", "a\nbcd", [ "a\nb" ] ],
  59. [ "%c\n\n", "x\n\n", [ "x" ] ],
  60. [ "%c", "\n", [ "\n" ] ],
  61. [ "%c", "x\n", [ "x" ] ],
  62. [ "%2c", " 123", [" 1"] ],
  63. [ " %c", " x", ["x"] ],
  64. [ "%c", " x", [" "] ],
  65. [ "%c", "123", ["1"] ],
  66. [ "%2c", "123", ["12"] ],
  67. [ "%5c", "a\nb\n\n", [ "a\nb\n\n" ] ],
  68. [ "%6c", "a\nb\n\nx", [ "a\nb\n\nx" ] ],
  69. [ "%5c", "ab\ncd", [ "ab\ncd" ] ],
  70. # Testing 'o'
  71. [ "%3o", "0xz", [0] ],
  72. # Testing 'd'
  73. [ "%d", "\n123", [ 123 ] ],
  74. [ "%d", "\n\n123", [ 123 ] ],
  75. [ "%1d", "2", [2] ],
  76. # Mixed tests
  77. # Includes:
  78. # whitespace/newline
  79. # mixed integer bases
  80. # various mixed specifiers
  81. [ "%[^\\w]%c", "...1", [ "...", "1"] ],
  82. [ "%[abc\n]%d", "a\n\nb\n\nc 123", [ "a\n\nb\n\nc", 123 ] ],
  83. [ "%[abc\n]%d", "a\n\nb\n\nc \t 123", [ "a\n\nb\n\nc", 123 ] ],
  84. [ "%[abc\t]%d", "a\t\tb\t\tc 123", [ "a\t\tb\t\tc", 123 ] ],
  85. [ "%d%3[abc\n]", "123a\nbeaab", [ 123, "a\nb" ] ],
  86. [ "%d%20c", "42 is the key", [ 42, " is the key" ] ],
  87. [ "%d %20c", "42 is the key", [ 42, "is the key" ] ],
  88. [ "%d%3[^abc\n]%d", "123de\nf123", [ 123, "de" ] ],
  89. [ "%d %4c", "3abc", [ 3, "abc" ] ],
  90. [ "%f%d\n%[abc]", "1\neabc", [1.0] ],
  91. [ "%d%3[abc]", "123aaab", [ 123, "aaa" ] ],
  92. [ "%d%3[abc]", "123 aaab", [ 123 ] ],
  93. [ "%d%3[abc]", "123aeaab", [ 123, "a" ] ],
  94. [ "%d%[^abc]", "123defabc", [123, "def" ] ],
  95. [ "%d%3[^abc]", "123defdef", [ 123, "def" ] ],
  96. [ "%d%3[^abc] ", "123defdef ", [ 123, "def" ] ],
  97. [ "%d%3[^abc]ghi", "123defghi", [ 123, "def" ] ],
  98. [ "%d%3[^abc]", "123adefdef", [ 123 ] ],
  99. [ "%d%3[^abc]", "123deafdef", [ 123, "de" ] ],
  100. [ "%d%3[^abc\n]", "123de\nf", [ 123, "de" ] ],
  101. [ "%s%c%c%s", "abc\n\ndef", ["abc", "\n","\n", "def" ] ],
  102. [ "%c%d", "\n\n123", [ "\n",123 ] ],
  103. [ "%s%c%d", "abc\n123", [ "abc", "\n", 123 ] ],
  104. [ "%s%c%d", "abc\n\n123", [ "abc", "\n", 123 ] ],
  105. [ "%c%d", "\t\n123", [ "\t",123 ] ],
  106. [ "%s%c%d", "abc\t\n123", [ "abc", "\t", 123 ] ],
  107. [ "%3c%d", "abc123", [ "abc", 123 ] ],
  108. [ "%3c\n%d", "abc123", [ "abc", 123 ] ],
  109. [ "%3c\n%d", "abc 123", [ "abc", 123 ] ],
  110. [ "%3c %d", "abc123", [ "abc", 123 ] ],
  111. [ "%3c\t%d", "abc \n 123", [ "abc", 123 ] ],
  112. [ "%3c\t%d", "abc \n 123 ", [ "abc", 123 ] ],
  113. [ "%3c%d", "a\nb123", [ "a\nb", 123 ] ],
  114. [ "%f%3c", "1.2x\ny", [ 1.2, "x\ny"] ],
  115. [ "%d\n%d\n%d", "123 456 789", [ 123,456,789 ] ],
  116. [ "%d\n%i%2d%x\n%d", "123 0718932", [ 123, 071, 89, 0x32] ],
  117. [ "%c\n%c", "x y", [ "x", "y" ] ],
  118. [ "%c\t%c", "x y", [ "x", "y" ] ],
  119. [ "%s\n%s", "x y", [ "x", "y" ] ],
  120. [ "%s%s\n", "x y", [ "x", "y" ] ],
  121. [ "%c\n\n%c", "x\n\ny", [ "x", "y" ] ],
  122. [ "%s%d%d", "abc\n123\n456", [ "abc", 123, 456 ] ],
  123. [ "%3s%c%3c%d", "1.2x\n\ny123", [ "1.2", "x", "\n\ny", 123 ] ],
  124. [ "%c\n%c", "x\n\ny", [ "x", "y" ] ],
  125. [ "%c %c", "x\n\ny", [ "x", "y" ] ],
  126. [ "%s\n\n%c", "x\n\ny", [ "x", "y" ] ],
  127. [ "%s\n\n%s", "x\n\ny", [ "x", "y" ] ],
  128. [ "%d\n\n%d", "23\n\n45", [ 23, 45 ] ],
  129. [ "%d\n%d", "23\n\n45", [ 23, 45 ] ],
  130. [ "%c\n\n%c", "x y", [ "x", "y" ] ],
  131. [ "%c%c", "x\n\ny", [ "x", "\n" ] ],
  132. [ "%c%c", "x\n", [ "x", "\n" ] ],
  133. [ "%d%c%c%d", "345 678", [ 345, " ", " ", 678] ],
  134. [ "%d %c%s", "123 x hello", [123, "x", "hello"] ],
  135. [ "%d%2c", "654 123", [654," 1"] ],
  136. [ "%5c%s", "a\nb\n\nxyz", [ "a\nb\n\n","xyz" ] ],
  137. [ "%s%[ xyz]%d", "hello x 32", ["hello", " x ", 32] ],
  138. [ "%5s%8[a-z]%d", "helloblahblah 32", ["hello", "blahblah", 32] ],
  139. [ '%s%[abcde\\s]%d', "hello badea 32", ["hello", " badea ", 32] ],
  140. [ '%d%[\\s]%c', "123 \n\t X", [ 123," \n\t ", "X"] ],
  141. [ "%4s%2c%c", "1.2x\n\ny", [ "1.2x", "\n\n","y"] ],
  142. [ "%f%c %3c%d", "1.2x\n\ny123", [ 1.2, "x", "y12", 3 ] ],
  143. [ "%s%5c", "abc ab\ncd", [ "abc", " ab\nc" ] ],
  144. [ "%5c%f", "ab\ncd1.2", [ "ab\ncd",1.2 ] ],
  145. [ "%5c%c", "ab\ncd1", [ "ab\ncd","1" ] ],
  146. [ "%f%c%2c%d", "1.2x\ny123", [ 1.2, "x", "\ny", 123 ] ],
  147. [ "%f%c%3c", "1.2x\ny123", [ 1.2, "x", "\ny1"] ],
  148. [ "%s\n%s", "blah\n\nand\nmore stuff", [ "blah", "and" ] ],
  149. [ "%o%d%x", "21912a3", [ "21".oct, 912, "a3".hex ] ],
  150. [ "%3o%4d%3x", "21912a3", [ "21".oct, 912, "a3".hex ] ],
  151. [ "%3o%4d%5x", "2191240xa3", [ "21".oct, 9124, "a3".hex ] ],
  152. [ "%3d%3x", "12abc", [12, "abc".hex] ],
  153. [ "%s%i%d", "hello +0xdef 123", [ "hello", "def".hex, 123] ],
  154. [ "%s%i%d", "hello -0xdef 123", [ "hello", -"def".hex, 123] ],
  155. [ "%s%i%i%i%i", "hello 012 -012 100 1", [ "hello", 10, -10, 100, 1 ] ],
  156. [ "%s%i%i%i%i", "hello 012 0x12 100 1", [ "hello", 10, 18, 100, 1 ] ],
  157. [ "%s%5i%3i%4i", "hello 0x123 123 0123", [ "hello", "0x123".hex, 123,"0123".oct] ],
  158. [ "%s%3i%4i", "hello 1230123", [ "hello", 123,"0123".oct] ],
  159. [ "%s%3i", "hello 1230", [ "hello", 123] ],
  160. [ "%s%5x%d", "hello 0xdef 123", [ "hello", "def".hex, 123] ],
  161. [ "%s%6x%d", "hello +0xdef 123", [ "hello", "def".hex, 123] ],
  162. [ "%s%6x%d", "hello -0xdef 123", [ "hello", -"def".hex, 123] ],
  163. [ "%s%4x%d", "hello -def 123", [ "hello", -"def".hex, 123] ],
  164. [ "%s%3x%d", "hello def 123", [ "hello", "def".hex, 123] ],
  165. [ "%s%x%d", "hello -def 123", [ "hello", -"def".hex, 123] ],
  166. [ "%s%x%d", "hello -0xdef 123", [ "hello", -"def".hex, 123] ],
  167. [ "%s%x%d", "hello 0xdef 123", [ "hello", "def".hex, 123] ],
  168. [ "%s%d%x%s", "hello 123 abc def", [ "hello", 123, "abc".hex, "def"] ],
  169. [ "%s%d%o%d", "hello 012 012 100", [ "hello", 12, 10, 100 ] ],
  170. [ "%s%d%o%d", "hello 012 -012 100", [ "hello", 12, -10, 100 ] ],
  171. [ "%s%o%x%d", "hello 012 0x12 100", [ "hello", 10, 18, 100 ] ],
  172. [ "%s%d%o%d", "hello 012 +01288", [ "hello", 12, 10, 88 ] ],
  173. [ "%f %d %s", "12.3e23 45 string", ["12.3e23".to_f, 45, "string"] ],
  174. [ "%f %d %s", "12.3e+23 45 string", ["12.3e23".to_f, 45, "string"] ],
  175. [ "%f %d %s", "12.3e-23 45 string", ["12.3e-23".to_f, 45, "string"] ],
  176. [ "%f %d %s", "-12.3e-23 45 string", ["-12.3e-23".to_f, 45, "string"] ],
  177. [ "%f %d %s", "12.e23 45 string", ["12.e23".to_f, 45, "string"] ],
  178. [ "%5f %d %s", "1.2e23 string", ["1.2e2".to_f, 3, "string"] ],
  179. [ "%5f%d %s", "1.2e23 string", ["1.2e2".to_f, 3, "string"] ],
  180. [ "%5f%d %d %s", "1.2e23 45 string", ["1.2e2".to_f, 3, 45, "string"] ],
  181. [ "%6f %d %d %s", "+1.2e23 45 string", ["1.2e2".to_f, 3, 45, "string"] ],
  182. [ "%d %d", "123 \n 345", [123, 345] ],
  183. [ "%d %*d", "123 \n 345", [123] ],
  184. [ "%d %3d789", "123 +45789", [123, 45] ],
  185. [ "%d %3d%d", "123 +456789", [123, 45, 6789] ],
  186. [ "%d %3dabc", "123 456abc", [123, 456] ],
  187. [ "%d %s", "123abc", [123, "abc"] ],
  188. [ "%d%s %s", "123 abc def", [123, "abc", "def"] ],
  189. [ "%s%s", "abc123 def", ["abc123", "def"] ],
  190. [ "%s%s %s", "123 abc def", ["123", "abc", "def"] ],
  191. [ "%s%%%s", "abc % def", ["abc", "def"] ],
  192. [ "%d %3d %s", "+123 456abc", [123, 456, "abc"] ],
  193. [ "%d %3d %s", "123 456abc", [123, 456, "abc"] ],
  194. [ "%d %3d %s", "123 +456 abc", [123, 45, "6"] ],
  195. [ "%d %3d %s", "-123-456abc", [-123, -45, "6abc"] ],
  196. [ "%dabc%d", "123abc345", [123, 345] ],
  197. [ "%d%5s%d", "123 abcde12", [123, "abcde", 12] ],
  198. [ "%5d%5s%5d", "12345abcde67890", [12345, "abcde", 67890] ],
  199. [ "%5d%*5s%5d", "12345abcde67890", [12345, 67890] ],
  200. [ " 12345%5s%5d", "12345abcde67890", [ "abcde", 67890] ],
  201. [ "%5dabcde%5d", "12345abcde67890", [ 12345, 67890] ],
  202. [ "%s%%%*s", "abc % def", ["abc"] ],
  203. [ "%*6s %d", "string 123", [123] ],
  204. [ "%d %*3d %s", "-123-456abc", [-123, "6abc"] ],
  205. [ "%d%s", "123", [123] ],
  206. [ "%s%d", "abc", ["abc"] ],
  207. [ "%f%x", "3.2e45x", ["3.2e45x".to_f] ],
  208. [ "%*5f%d %d %s", "1.2e23 45 string", [3, 45, "string"] ],
  209. [ "%5f%*d %d %s", "1.2e23 45 string", ["1.2e2".to_f, 45, "string"] ],
  210. [ "%*5f%*d %*d %s", "1.2e23 45 string", ["string"] ],
  211. [ "%f %*d %s", "12.e23 45 string", ["12.e23".to_f, "string"] ],
  212. [ "%s %f %s %d %x%c%c%c%c",
  213. "float: 1.2e23 dec/hex: 135a23 abc",
  214. ["float:", "1.2e23".to_f, "dec/hex:", 135, "a23".hex, " ", "a", "b", "c" ] ],
  215. # Testing 's'
  216. [ "%s\n", "blah\n\n\n", [ "blah" ] ],
  217. # Testing '['
  218. [ "%[a\nb]", "a\nb", [ "a\nb" ] ],
  219. [ "%[abc]", "acb", [ "acb" ] ],
  220. [ "%[abc\n]", "a\nb", [ "a\nb" ] ],
  221. [ "%[^abc]", "defabc", [ "def" ] ],
  222. [ "%[-abc]", "abc-cba", [ "abc-cba" ] ],
  223. [ "%[\n]", "\n", [ "\n" ] ],
  224. [ "%[\n]", "\nabc", [ "\n" ] ],
  225. [ "%[\n\t]", "\t\n", [ "\t\n" ] ],
  226. [ "%[a-f]", "abczef", [ "abc" ] ],
  227. [ "%d%3[[:lower:]] %f", "123ade1.2", [ 123,"ade",1.2 ] ],
  228. [ "%d%3[[:lower:]] %f", "123ad1.2", [ 123,"ad",1.2 ] ],
  229. [ "%d%3[[:lower:]] %f", "123 ad1.2", [ 123 ] ],
  230. [ "%d%[[:lower:]]", "123abcdef1.2", [ 123, "abcdef" ] ],
  231. [ "%[[:lower:]]%d", "abcdef123", [ "abcdef", 123 ] ],
  232. [ "%[[:digit:]]%[[:alpha:]]", "123abcdef", [ "123", "abcdef" ] ],
  233. [ "%[[:digit:]]%d", "123 123", [ "123", 123 ] ],
  234. [ "%[[:upper:]]", "ABCdefGHI", [ "ABC" ] ],
  235. # Testing 'f'
  236. [ "%2f", "x", [] ],
  237. [ "%F", "1.23e45", [1.23e+45] ],
  238. [ "%e", "3.25ee", [3.25] ],
  239. [ "%E", "3..25", [3.0] ],
  240. [ "%g", "+3.25", [3.25] ],
  241. [ "%G", "+3.25e2", [325.0] ],
  242. [ "%f", "3.z", [3.0] ],
  243. [ "%a", "0X1P+10", [1024.0] ],
  244. [ "%A", "0x1.deadbeefp+99", [1.1851510441583988e+30] ],
  245. # Testing embedded matches including literal '[' behavior
  246. [",%d,%f", ",10,1.1", [10,1.1] ],
  247. [" ,%d,%f", " ,10,1.1", [10,1.1] ],
  248. ["[%d,%f", "[10,1.1", [10,1.1] ],
  249. [" [%d,%f", " [10,1.1", [10,1.1] ],
  250. ]
  251. end
  252. def each_test
  253. self.tests.each do |test|
  254. format, string, = test
  255. yield test, "#{string.dump}(#{format.dump})"
  256. end
  257. end
  258. end
  259. class TestStringScanf
  260. include Scanf
  261. extend ScanfTests
  262. self.each_test do |test, i|
  263. define_method("test_#{i}") do ||
  264. assert_equal(test[2], test[1].scanf(test[0]))
  265. end
  266. end
  267. end
  268. class TestIOScanf
  269. include Scanf
  270. extend ScanfTests
  271. self.each_test do |test, i|
  272. define_method("test_#{i}") do ||
  273. Tempfile.create("iotest.dat") do |fh|
  274. fh.print test[1]
  275. fh.rewind
  276. assert_equal(test[2], fh.scanf(test[0]))
  277. end
  278. end
  279. end
  280. end