1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH << __dir__
5
+
6
+ require 'helper'
7
+
8
+ # # This is to force testing 'rails_friendly_size'.
9
+ # begin
10
+ # require 'active_support'
11
+ # HAS_RAILS = true
12
+ # rescue LoadError
13
+ # puts 'ActiveSupport not found. Skipping ActiveSupport tests.'
14
+ # HAS_RAILS = false
15
+ # end
16
+
17
+ # The tests in this file are to specifically handle testing the ARM Neon code
18
+ # that is used to speed up the dumping of long strings. The tests are likely
19
+ # redundant with respect to correctness. However, they are designed specifically
20
+ # to exercise the code paths that operate on vectors of 16 bytes. Additionally,
21
+ # we need to ensure that not all tests are exactly multiples of 16 bytes in length
22
+ # so that we can test the code that handles the remainder of the string.
23
+ class LongStringsTest < Minitest ::Test
24
+
25
+ def test_escapes
26
+ run_basic_tests ( :compat )
27
+ run_basic_tests ( :rails )
28
+
29
+ # if HAS_RAILS
30
+ # Oj.optimize_rails()
31
+ # ActiveSupport::JSON::Encoding.escape_html_entities_in_json = false
32
+ # run_basic_tests(:rails)
33
+ # end
34
+ end
35
+
36
+ def run_basic_tests ( mode )
37
+ str = '\n' *15
38
+ expected = "\" #{ '\\\\n' *15 } \" "
39
+ out = Oj . dump ( str , mode : mode )
40
+ assert_equal ( expected , out )
41
+
42
+ str = '\n' *16
43
+ expected = "\" #{ '\\\\n' *16 } \" "
44
+ out = Oj . dump ( str , mode : mode )
45
+ assert_equal ( expected , out )
46
+
47
+ str = '\n' *17
48
+ expected = "\" #{ '\\\\n' *17 } \" "
49
+ out = Oj . dump ( str , mode : mode )
50
+ assert_equal ( expected , out )
51
+
52
+ str = '\n' *1700
53
+ expected = "\" #{ '\\\\n' *1700 } \" "
54
+ out = Oj . dump ( str , mode : mode )
55
+ assert_equal ( expected , out )
56
+
57
+ str = '\n' *32
58
+ expected = "\" #{ '\\\\n' *32 } \" "
59
+ out = Oj . dump ( str , mode : mode )
60
+ assert_equal ( expected , out )
61
+
62
+ str = '\f' *63
63
+ expected = "\" #{ '\\\\f' *63 } \" "
64
+ out = Oj . dump ( str , mode : mode )
65
+ assert_equal ( expected , out )
66
+
67
+ str = '\t' *127
68
+ expected = "\" #{ '\\\\t' *127 } \" "
69
+ out = Oj . dump ( str , mode : mode )
70
+ assert_equal ( expected , out )
71
+
72
+ str = '\t' *500
73
+ expected = "\" #{ '\\\\t' *500 } \" "
74
+ out = Oj . dump ( str , mode : mode )
75
+ assert_equal ( expected , out )
76
+
77
+ str = "\u0001 " * 16
78
+ out = Oj . dump ( str , mode : mode )
79
+ expected = "\" #{ "\\ u0001" * 16 } \" "
80
+ assert_equal ( expected , out )
81
+
82
+ str = "\u0001 " * 1024
83
+ out = Oj . dump ( str , mode : mode )
84
+ expected = "\" #{ "\\ u0001" * 1024 } \" "
85
+ assert_equal ( expected , out )
86
+
87
+ str = "\u0001 \u0002 " * 8
88
+ out = Oj . dump ( str , mode : mode )
89
+ expected = "\" #{ "\\ u0001\\ u0002" * 8 } \" "
90
+ assert_equal ( expected , out )
91
+
92
+ str = "\u0001 \u0002 " * 2000
93
+ out = Oj . dump ( str , mode : mode )
94
+ expected = "\" #{ "\\ u0001\\ u0002" * 2000 } \" "
95
+ assert_equal ( expected , out )
96
+
97
+ str = "\u0001 a" * 8
98
+ out = Oj . dump ( str , mode : mode )
99
+ expected = "\" #{ "\\ u0001a" * 8 } \" "
100
+ assert_equal ( expected , out )
101
+
102
+ str = "abc\u0010 " * 4
103
+ out = Oj . dump ( str , mode : mode )
104
+ expected = "\" #{ "abc\\ u0010" * 4 } \" "
105
+ assert_equal ( expected , out )
106
+
107
+ str = "abc\u0010 " * 5
108
+ out = Oj . dump ( str , mode : mode )
109
+ expected = "\" #{ "abc\\ u0010" * 5 } \" "
110
+ assert_equal ( expected , out )
111
+
112
+ str = "\u0001 \u0002 " * 9
113
+ out = Oj . dump ( str , mode : mode )
114
+ expected = "\" #{ "\\ u0001\\ u0002" * 9 } \" "
115
+ assert_equal ( expected , out )
116
+
117
+ str = "\u0001 \u0002 " * 2048
118
+ out = Oj . dump ( str , mode : mode )
119
+ expected = "\" #{ "\\ u0001\\ u0002" * 2048 } \" "
120
+ assert_equal ( expected , out )
121
+
122
+ str = '\"'
123
+ out = Oj . dump ( str , mode : mode )
124
+ expected = "\" \\ \\ \\ \" \" "
125
+ assert_equal ( expected , out )
126
+
127
+ str = '"' *16
128
+ out = Oj . dump ( str , mode : mode )
129
+ expected = '"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\""'
130
+ assert_equal ( expected , out )
131
+
132
+ str = '"' *20
133
+ out = Oj . dump ( str , mode : mode )
134
+ expected = '"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\"\\""'
135
+ assert_equal ( expected , out )
136
+ end
137
+
138
+ def test_dump_long_str_no_escapes
139
+ str = 'This is a test of the emergency broadcast system. This is only a test.'
140
+ out = Oj . dump ( str )
141
+ assert_equal ( %|"#{ str } "| , out )
142
+ end
143
+
144
+ def test_dump_long_str_with_escapes
145
+ str = 'This is a\ntest of the emergency broadcast system. This is only a test.'
146
+ out = Oj . dump ( str )
147
+ expected = %|"This is a\\ \\ ntest of the emergency broadcast system. This is only a test."|
148
+ assert_equal ( expected , out )
149
+ end
150
+
151
+ def test_dump_long_str_with_quotes
152
+ str = 'This is a "test" of the emergency broadcast system. This is only a "test".'
153
+ out = Oj . dump ( str )
154
+ expected = %|"This is a \\ \" test\\ \" of the emergency broadcast system. This is only a \\ \" test\\ \" ."|
155
+ assert_equal ( expected , out )
156
+ end
157
+
158
+ def test_dump_long_str_no_escapes_rails
159
+ str = 'This is a test of the emergency broadcast system. This is only a test.'
160
+ out = Oj . dump ( str , mode : :rails )
161
+ assert_equal ( %|"#{ str } "| , out )
162
+ end
163
+
164
+ def test_dump_long_str_with_escapes_rails
165
+ str = 'This is a\ntest of the emergency broadcast system. This is only a test.'
166
+ out = Oj . dump ( str , mode : :rails )
167
+ expected = %|"This is a\\ \\ ntest of the emergency broadcast system. This is only a test."|
168
+ assert_equal ( expected , out )
169
+ end
170
+
171
+ def test_dump_long_str_with_quotes_rails
172
+ str = 'This is a "test" of the emergency broadcast system. This is only a "test".'
173
+ out = Oj . dump ( str , mode : :rails )
174
+ expected = %|"This is a \\ \" test\\ \" of the emergency broadcast system. This is only a \\ \" test\\ \" ."|
175
+ assert_equal ( expected , out )
176
+ end
177
+
178
+ def test_long_string_with_high_byte_set
179
+ str = 'This item will cost €1000.00. I hope you have a great day!'
180
+ out = Oj . dump ( str )
181
+ expected = %["This item will cost €1000.00. I hope you have a great day!"]
182
+ assert_equal ( expected , out )
183
+
184
+ out = Oj . dump ( str , mode : :rails )
185
+ assert_equal ( expected , out )
186
+ end
187
+
188
+ def test_high_byte_set
189
+ str = "€" *15
190
+ out = Oj . dump ( str )
191
+ expected = %["#{ "€" *15 } "]
192
+ assert_equal ( expected , out )
193
+ out = Oj . dump ( str , mode : :rails )
194
+ assert_equal ( expected , out )
195
+
196
+ str = "€" *16
197
+ out = Oj . dump ( str )
198
+ expected = %["#{ "€" *16 } "]
199
+ assert_equal ( expected , out )
200
+ out = Oj . dump ( str , mode : :rails )
201
+ assert_equal ( expected , out )
202
+
203
+ str = "€" *17
204
+ out = Oj . dump ( str )
205
+ expected = %["#{ "€" *17 } "]
206
+ assert_equal ( expected , out )
207
+ out = Oj . dump ( str , mode : :rails )
208
+ assert_equal ( expected , out )
209
+
210
+ str = "€" *1700
211
+ out = Oj . dump ( str )
212
+ expected = %["#{ "€" *1700 } "]
213
+ assert_equal ( expected , out )
214
+ out = Oj . dump ( str , mode : :rails )
215
+ assert_equal ( expected , out )
216
+
217
+ str = "€abcdefghijklmnop"
218
+ out = Oj . dump ( str )
219
+ expected = %["#{ "€abcdefghijklmnop" } "]
220
+ assert_equal ( expected , out )
221
+ out = Oj . dump ( str , mode : :rails )
222
+ assert_equal ( expected , out )
223
+
224
+ str = "€" + "abcdefghijklmnop" *1000
225
+ out = Oj . dump ( str )
226
+ expected = %["#{ "€" + "abcdefghijklmnop" *1000 } "]
227
+ assert_equal ( expected , out )
228
+ out = Oj . dump ( str , mode : :rails )
229
+ assert_equal ( expected , out )
230
+
231
+ str = "€" + "abcdefghijklmnop" * 2000 + "€"
232
+ out = Oj . dump ( str )
233
+ expected = %["#{ "€" + "abcdefghijklmnop" * 2000 + "€" } "]
234
+ assert_equal ( expected , out )
235
+ out = Oj . dump ( str , mode : :rails )
236
+ assert_equal ( expected , out )
237
+
238
+ str = "abcdefghijklmnop" *3000 + "€"
239
+ out = Oj . dump ( str )
240
+ expected = %["#{ "abcdefghijklmnop" *3000 + "€" } "]
241
+ assert_equal ( expected , out )
242
+ out = Oj . dump ( str , mode : :rails )
243
+ assert_equal ( expected , out )
244
+ end
245
+ end
0 commit comments