1
0

test-instream.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. TEST(instream) {
  2. {
  3. InStream s(inf, "this 100500 the test 123\n\n");
  4. ensure(s.readWord() == "this");
  5. ensure(s.readInt() == 100500);
  6. ensure(s.readToken() == "the");
  7. ensure(s.readLine() == " test 123");
  8. ensure(s.readLine() == "");
  9. ensure(s.eof());
  10. ensure_exit(3, [&](){s.readWord();});
  11. }
  12. {
  13. ensure_exit(3, [](){InStream(inf, "").readWord();});
  14. }
  15. {
  16. InStream s(inf, "3\n10 20 30\n");
  17. ensure(s.readInt(1, 3, "n") == 3);
  18. ensure(s.readInts(3, 10, 30, "a") == vector<int>({10, 20, 30}));
  19. }
  20. {
  21. ensure_exit(3, [](){InStream(inf, "1").readInt(0, 0, "n");});
  22. ensure_exit(3, [](){InStream(inf, "abacaba").readToken("[abc]{6}", "n");});
  23. ensure(InStream(inf, "abacaba").readToken("[abc]{7}", "s") == "abacaba");
  24. ensure(InStream(inf, "abacaba").readToken("a|test|abacaba|ok|abac", "s") == "abacaba");
  25. ensure_exit(3, [](){InStream(inf, "abacab").readToken("a|test|abacaba|ok|abac", "n");});
  26. ensure_exit(3, [](){InStream(inf, "abacabaa").readToken("a|test|abacaba|ok|abac", "n");});
  27. ensure_exit(3, [](){InStream(inf, "abacaba!").readToken("a|test|abacaba|ok|abac", "n");});
  28. InStream(inf, "1 2 3 4 1").readInts(5, 1, 4, "a");
  29. ensure_exit(3, [](){InStream(inf, "1 2 3 4 1").readInts(5, 1, 3, "a");});
  30. {
  31. string result;
  32. InStream(inf, "\t\t100500\t").readTokenTo(result, "[0-9]{1,6}", "n");
  33. ensure(result == "100500");
  34. }
  35. }
  36. {
  37. InStream s(inf, "\xEF\xBB\xBF" "content");
  38. s.skipBom();
  39. ensure(s.readWord() == "content");
  40. ensure(s.eof());
  41. }
  42. {
  43. InStream s(inf, "content");
  44. s.skipBom();
  45. ensure(s.readWord() == "content");
  46. ensure(s.eof());
  47. }
  48. {
  49. InStream s(inf, "\xEF" "content");
  50. s.skipBom();
  51. ensure(s.readChar() == '\xEF');
  52. ensure(s.readWord() == "content");
  53. ensure(s.eof());
  54. }
  55. }