PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/t/base/rs.t

https://github.com/rurban/perl
Perl | 298 lines | 237 code | 29 blank | 32 comment | 28 complexity | aa8c7ea225b84ce832ab84d05673e7f2 MD5 | raw file
Possible License(s): AGPL-1.0
  1. #!./perl
  2. # Test $/
  3. print "1..39\n";
  4. $test_count = 1;
  5. $teststring = "1\n12\n123\n1234\n1234\n12345\n\n123456\n1234567\n";
  6. $teststring2 = "1234567890123456789012345678901234567890";
  7. # Create our test datafile
  8. 1 while unlink 'foo'; # in case junk left around
  9. rmdir 'foo';
  10. open TESTFILE, ">./foo" or die "error $! $^E opening";
  11. binmode TESTFILE;
  12. print TESTFILE $teststring;
  13. close TESTFILE or die "error $! $^E closing";
  14. $test_count_start = $test_count; # Needed to know how many tests to skip
  15. open TESTFILE, "<./foo";
  16. binmode TESTFILE;
  17. test_string(*TESTFILE);
  18. close TESTFILE;
  19. unlink "./foo";
  20. # try the record reading tests. New file so we don't have to worry about
  21. # the size of \n.
  22. open TESTFILE, ">./foo";
  23. print TESTFILE $teststring2;
  24. binmode TESTFILE;
  25. close TESTFILE;
  26. open TESTFILE, "<./foo";
  27. binmode TESTFILE;
  28. test_record(*TESTFILE);
  29. close TESTFILE;
  30. $test_count_end = $test_count; # Needed to know how many tests to skip
  31. $/ = "\n";
  32. my $note = "\$/ preserved when set to bad value";
  33. # none of the setting of $/ to bad values should modify its value
  34. test_bad_setting();
  35. print +($/ ne "\n" ? "not " : "") .
  36. "ok $test_count # \$/ preserved when set to bad value\n";
  37. ++$test_count;
  38. # Now for the tricky bit--full record reading
  39. if ($^O eq 'VMS') {
  40. # Create a temp file. We jump through these hoops 'cause CREATE really
  41. # doesn't like our methods for some reason.
  42. open FDLFILE, "> ./foo.fdl";
  43. print FDLFILE "RECORD\n FORMAT VARIABLE\n";
  44. close FDLFILE;
  45. open CREATEFILE, "> ./foo.com";
  46. print CREATEFILE '$ DEFINE/USER SYS$INPUT NL:', "\n";
  47. print CREATEFILE '$ DEFINE/USER SYS$OUTPUT NL:', "\n";
  48. print CREATEFILE '$ OPEN YOW []FOO.BAR/WRITE', "\n";
  49. print CREATEFILE '$ CLOSE YOW', "\n";
  50. print CREATEFILE "\$EXIT\n";
  51. close CREATEFILE;
  52. $throwaway = `\@\[\]foo`, "\n";
  53. open(TEMPFILE, ">./foo.bar") or print "# open failed $! $^E\n";
  54. print TEMPFILE "foo\nfoobar\nbaz\n";
  55. close TEMPFILE;
  56. open TESTFILE, "<./foo.bar";
  57. $/ = \10;
  58. $bar = <TESTFILE>;
  59. if ($bar eq "foo\n") {print "ok $test_count\n";} else {print "not ok $test_count\n";}
  60. $test_count++;
  61. $bar = <TESTFILE>;
  62. if ($bar eq "foobar\n") {print "ok $test_count\n";} else {print "not ok $test_count\n";}
  63. $test_count++;
  64. # can we do a short read?
  65. $/ = \2;
  66. $bar = <TESTFILE>;
  67. if ($bar eq "ba") {print "ok $test_count\n";} else {print "not ok $test_count\n";}
  68. $test_count++;
  69. # do we get the rest of the record?
  70. $bar = <TESTFILE>;
  71. if ($bar eq "z\n") {print "ok $test_count\n";} else {print "not ok $test_count\n";}
  72. $test_count++;
  73. close TESTFILE;
  74. 1 while unlink qw(foo.bar foo.com foo.fdl);
  75. } else {
  76. # Nobody else does this at the moment (well, maybe OS/390, but they can
  77. # put their own tests in) so we just punt
  78. foreach $test ($test_count..$test_count + 3) {
  79. print "ok $test # skipped on non-VMS system\n";
  80. $test_count++;
  81. }
  82. }
  83. $/ = "\n";
  84. # see if open/readline/close work on our and my variables
  85. {
  86. if (open our $T, "./foo") {
  87. my $line = <$T>;
  88. print "# $line\n";
  89. length($line) == 40 or print "not ";
  90. close $T or print "not ";
  91. }
  92. else {
  93. print "not ";
  94. }
  95. print "ok $test_count # open/readline/close on our variable\n";
  96. $test_count++;
  97. }
  98. {
  99. if (open my $T, "./foo") {
  100. my $line = <$T>;
  101. print "# $line\n";
  102. length($line) == 40 or print "not ";
  103. close $T or print "not ";
  104. }
  105. else {
  106. print "not ";
  107. }
  108. print "ok $test_count # open/readline/close on my variable\n";
  109. $test_count++;
  110. }
  111. {
  112. # If we do not include the lib directories, we may end up picking up a
  113. # binary-incompatible previously-installed version. The eval won’t help in
  114. # intercepting a SIGTRAP.
  115. local @INC = ("../lib", "lib", @INC);
  116. if (not eval q/use PerelIO::scalar; 1/) {
  117. # In-memory files necessitate PerlIO::scalar, thus a perl with
  118. # perlio and dynaloading enabled. miniperl won't be able to run this
  119. # test, so skip it
  120. for $test ($test_count .. $test_count + ($test_count_end - $test_count_start - 1)) {
  121. print "ok $test # skipped - Can't test in memory file with miniperl/without PerlIO::Scalar\n";
  122. $test_count++;
  123. }
  124. }
  125. else {
  126. # Test if a file in memory behaves the same as a real file (= re-run the test with a file in memory)
  127. open TESTFILE, "<", \$teststring;
  128. test_string(*TESTFILE);
  129. close TESTFILE;
  130. open TESTFILE, "<", \$teststring2;
  131. test_record(*TESTFILE);
  132. close TESTFILE;
  133. }
  134. }
  135. # Get rid of the temp file
  136. END { unlink "./foo"; }
  137. sub test_string {
  138. *FH = shift;
  139. # Check the default $/
  140. $bar = <FH>;
  141. if ($bar ne "1\n") {print "not ";}
  142. print "ok $test_count # default \$/\n";
  143. $test_count++;
  144. # explicitly set to \n
  145. $/ = "\n";
  146. $bar = <FH>;
  147. if ($bar ne "12\n") {print "not ";}
  148. print "ok $test_count # \$/ = \"\\n\"\n";
  149. $test_count++;
  150. # Try a non line terminator
  151. $/ = 3;
  152. $bar = <FH>;
  153. if ($bar ne "123") {print "not ";}
  154. print "ok $test_count # \$/ = 3\n";
  155. $test_count++;
  156. # Eat the line terminator
  157. $/ = "\n";
  158. $bar = <FH>;
  159. # How about a larger terminator
  160. $/ = "34";
  161. $bar = <FH>;
  162. if ($bar ne "1234") {print "not ";}
  163. print "ok $test_count # \$/ = \"34\"\n";
  164. $test_count++;
  165. # Eat the line terminator
  166. $/ = "\n";
  167. $bar = <FH>;
  168. # Does paragraph mode work?
  169. $/ = '';
  170. $bar = <FH>;
  171. if ($bar ne "1234\n12345\n\n") {print "not ";}
  172. print "ok $test_count # \$/ = ''\n";
  173. $test_count++;
  174. # Try slurping the rest of the file
  175. $/ = undef;
  176. $bar = <FH>;
  177. if ($bar ne "123456\n1234567\n") {print "not ";}
  178. print "ok $test_count # \$/ = undef\n";
  179. $test_count++;
  180. }
  181. sub test_record {
  182. *FH = shift;
  183. # Test straight number
  184. $/ = \2;
  185. $bar = <FH>;
  186. if ($bar ne "12") {print "not ";}
  187. print "ok $test_count # \$/ = \\2\n";
  188. $test_count++;
  189. # Test stringified number
  190. $/ = \"2";
  191. $bar = <FH>;
  192. if ($bar ne "34") {print "not ";}
  193. print "ok $test_count # \$/ = \"2\"\n";
  194. $test_count++;
  195. # Integer variable
  196. $foo = 2;
  197. $/ = \$foo;
  198. $bar = <FH>;
  199. if ($bar ne "56") {print "not ";}
  200. print "ok $test_count # \$/ = \\\$foo (\$foo = 2)\n";
  201. $test_count++;
  202. # String variable
  203. $foo = "2";
  204. $/ = \$foo;
  205. $bar = <FH>;
  206. if ($bar ne "78") {print "not ";}
  207. print "ok $test_count # \$/ = \\\$foo (\$foo = \"2\")\n";
  208. $test_count++;
  209. # Naughty straight number - should get the rest of the file
  210. # no warnings 'deprecated'; # but not in t/base/*
  211. { local $SIG{__WARN__} = sub {}; $/ = \0 }
  212. $bar = <FH>;
  213. if ($bar ne "90123456789012345678901234567890") {print "not ";}
  214. print "ok $test_count # \$/ = \\0\n";
  215. $test_count++;
  216. }
  217. sub test_bad_setting {
  218. if (eval {$/ = []; 1}) {
  219. print "not ok ",$test_count++," # \$/ = []; should die\n";
  220. print "not ok ",$test_count++," # \$/ = []; produced expected error message\n";
  221. } else {
  222. my $msg= $@ || "Zombie Error";
  223. print "ok ",$test_count++," # \$/ = []; should die\n";
  224. if ($msg!~m!Setting \$\/ to an ARRAY reference is forbidden!) {
  225. print "not ";
  226. }
  227. print "ok ",$test_count++," # \$/ = []; produced expected error message\n";
  228. }
  229. if (eval {$/ = {}; 1}) {
  230. print "not ok ",$test_count++," # \$/ = {}; should die\n";
  231. print "not ok ",$test_count++," # \$/ = {}; produced expected error message\n";
  232. } else {
  233. my $msg= $@ || "Zombie Error";
  234. print "ok ",$test_count++," # \$/ = {}; should die\n";
  235. if ($msg!~m!Setting \$\/ to a HASH reference is forbidden!) {print "not ";}
  236. print "ok ",$test_count++," # \$/ = {}; produced expected error message\n";
  237. }
  238. if (eval {$/ = \\1; 1}) {
  239. print "not ok ",$test_count++," # \$/ = \\\\1; should die\n";
  240. print "not ok ",$test_count++," # \$/ = \\\\1; produced expected error message\n";
  241. } else {
  242. my $msg= $@ || "Zombie Error";
  243. print "ok ",$test_count++," # \$/ = \\\\1; should die\n";
  244. if ($msg!~m!Setting \$\/ to a REF reference is forbidden!) {print "not ";}
  245. print "ok ",$test_count++," # \$/ = \\\\1; produced expected error message\n";
  246. }
  247. if (eval {$/ = qr/foo/; 1}) {
  248. print "not ok ",$test_count++," # \$/ = qr/foo/; should die\n";
  249. print "not ok ",$test_count++," # \$/ = qr/foo/; produced expected error message\n";
  250. } else {
  251. my $msg= $@ || "Zombie Error";
  252. print "ok ",$test_count++," # \$/ = qr/foo/; should die\n";
  253. if ($msg!~m!Setting \$\/ to a REGEXP reference is forbidden!) {print "not ";}
  254. print "ok ",$test_count++," # \$/ = qr/foo/; produced expected error message\n";
  255. }
  256. if (eval {$/ = \*STDOUT; 1}) {
  257. print "not ok ",$test_count++," # \$/ = \\*STDOUT; should die\n";
  258. print "not ok ",$test_count++," # \$/ = \\*STDOUT; produced expected error message\n";
  259. } else {
  260. my $msg= $@ || "Zombie Error";
  261. print "ok ",$test_count++," # \$/ = \\*STDOUT; should die\n";
  262. if ($msg!~m!Setting \$\/ to a GLOB reference is forbidden!) {print "not ";}
  263. print "ok ",$test_count++," # \$/ = \\*STDOUT; produced expected error message\n";
  264. }
  265. }