You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.9 KiB

пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 14 година
пре 13 година
пре 14 година
пре 14 година
пре 14 година
пре 13 година
пре 14 година
пре 13 година
пре 14 година
пре 13 година
пре 14 година
пре 13 година
пре 14 година
пре 13 година
пре 14 година
пре 13 година
пре 14 година
пре 13 година
пре 14 година
  1. #! /usr/bin/env escript
  2. % This file is part of Jiffy released under the MIT license.
  3. % See the LICENSE file for more information.
  4. main([]) ->
  5. code:add_pathz("ebin"),
  6. code:add_pathz("test_etap"),
  7. etap:plan(83),
  8. util:test_good(good()),
  9. util:test_good(uescaped(), [uescape]),
  10. util:test_errors(errors()),
  11. test_utf8(utf8_cases()),
  12. etap:end_tests().
  13. good() ->
  14. [
  15. {<<"\"\"">>, <<"">>},
  16. {<<"\"0\"">>, <<"0">>},
  17. {<<"\"foo\"">>, <<"foo">>},
  18. {<<"\"\\\"foobar\\\"\"">>, <<"\"foobar\"">>},
  19. {<<"\"\\n\\n\\n\"">>, <<"\n\n\n">>},
  20. {<<"\"\\\" \\b\\f\\r\\n\\t\\\"\"">>, <<"\" \b\f\r\n\t\"">>},
  21. {<<"\"foo\\u0005bar\"">>, <<"foo", 5, "bar">>},
  22. {
  23. <<"\"\\uD834\\uDD1E\"">>,
  24. <<240, 157, 132, 158>>,
  25. <<34, 240, 157, 132, 158, 34>>
  26. }
  27. ].
  28. uescaped() ->
  29. [
  30. {
  31. <<"\"\\u8CA8\\u5481\\u3002\\u0091\\u0091\"">>,
  32. <<232,178,168,229,146,129,227,128,130,194,145,194,145>>
  33. }
  34. ].
  35. errors() ->
  36. [
  37. <<"\"foo">>,
  38. <<"\"", 0, "\"">>,
  39. <<"\"\\g\"">>,
  40. <<"\"\\uFFFF\"">>,
  41. <<"\"\\uFFFE\"">>,
  42. <<"\"\\uD834foo\\uDD1E\"">>,
  43. % CouchDB-345
  44. <<34,78,69,73,77,69,78,32,70,216,82,82,32,70,65,69,78,33,34>>
  45. ].
  46. test_utf8([]) ->
  47. ok;
  48. test_utf8([Case | Rest]) ->
  49. etap:fun_is(
  50. fun({error, invalid_string}) -> true; (Else) -> Else end,
  51. (catch jiffy:encode(Case)),
  52. lists:flatten(io_lib:format("Invalid utf-8: ~p", [Case]))
  53. ),
  54. Case2 = <<34, Case/binary, 34>>,
  55. etap:fun_is(
  56. fun({error, {_, invalid_string}}) -> true; (Else) -> Else end,
  57. (catch jiffy:decode(Case2)),
  58. lists:flatten(io_lib:format("Invalid utf-8: ~p", [Case2]))
  59. ),
  60. test_utf8(Rest).
  61. utf8_cases() ->
  62. [
  63. % Stray continuation byte
  64. <<16#C2, 16#81, 16#80>>,
  65. <<"foo", 16#80, "bar">>,
  66. % Invalid Unicode code points
  67. <<239, 191, 190>>,
  68. <<237, 160, 129>>,
  69. % Not enough extension bytes
  70. <<16#C0>>,
  71. <<16#E0>>,
  72. <<16#E0, 16#80>>,
  73. <<16#F0>>,
  74. <<16#F0, 16#80>>,
  75. <<16#F0, 16#80, 16#80>>,
  76. <<16#F8>>,
  77. <<16#F8, 16#80>>,
  78. <<16#F8, 16#80, 16#80>>,
  79. <<16#F8, 16#80, 16#80, 16#80>>,
  80. <<16#FC>>,
  81. <<16#FC, 16#80>>,
  82. <<16#FC, 16#80, 16#80>>,
  83. <<16#FC, 16#80, 16#80, 16#80>>,
  84. <<16#FC, 16#80, 16#80, 16#80, 16#80>>,
  85. % No data in high bits.
  86. <<16#C0, 16#80>>,
  87. <<16#C1, 16#80>>,
  88. <<16#E0, 16#80, 16#80>>,
  89. <<16#E0, 16#90, 16#80>>,
  90. <<16#F0, 16#80, 16#80, 16#80>>,
  91. <<16#F0, 16#88, 16#80, 16#80>>,
  92. <<16#F8, 16#80, 16#80, 16#80, 16#80>>,
  93. <<16#F8, 16#84, 16#80, 16#80, 16#80>>,
  94. <<16#FC, 16#80, 16#80, 16#80, 16#80, 16#80>>,
  95. <<16#FC, 16#82, 16#80, 16#80, 16#80, 16#80>>
  96. ].