oj 3.13.23 → 3.16.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +81 -0
  3. data/README.md +2 -2
  4. data/ext/oj/buf.h +7 -6
  5. data/ext/oj/cache.c +29 -26
  6. data/ext/oj/cache.h +3 -2
  7. data/ext/oj/cache8.c +10 -9
  8. data/ext/oj/circarray.c +7 -5
  9. data/ext/oj/circarray.h +2 -2
  10. data/ext/oj/code.c +5 -12
  11. data/ext/oj/code.h +2 -2
  12. data/ext/oj/compat.c +20 -60
  13. data/ext/oj/custom.c +26 -59
  14. data/ext/oj/debug.c +3 -9
  15. data/ext/oj/dump.c +103 -53
  16. data/ext/oj/dump.h +1 -4
  17. data/ext/oj/dump_compat.c +557 -592
  18. data/ext/oj/dump_leaf.c +3 -5
  19. data/ext/oj/dump_object.c +42 -48
  20. data/ext/oj/dump_strict.c +10 -22
  21. data/ext/oj/encoder.c +1 -1
  22. data/ext/oj/err.c +2 -13
  23. data/ext/oj/err.h +9 -12
  24. data/ext/oj/extconf.rb +16 -7
  25. data/ext/oj/fast.c +60 -92
  26. data/ext/oj/intern.c +62 -47
  27. data/ext/oj/intern.h +3 -7
  28. data/ext/oj/mem.c +318 -0
  29. data/ext/oj/mem.h +53 -0
  30. data/ext/oj/mimic_json.c +51 -32
  31. data/ext/oj/object.c +33 -43
  32. data/ext/oj/odd.c +8 -6
  33. data/ext/oj/odd.h +4 -4
  34. data/ext/oj/oj.c +243 -212
  35. data/ext/oj/oj.h +83 -81
  36. data/ext/oj/parse.c +94 -148
  37. data/ext/oj/parse.h +21 -24
  38. data/ext/oj/parser.c +80 -67
  39. data/ext/oj/parser.h +7 -8
  40. data/ext/oj/rails.c +70 -92
  41. data/ext/oj/reader.c +9 -14
  42. data/ext/oj/reader.h +4 -2
  43. data/ext/oj/resolve.c +3 -4
  44. data/ext/oj/rxclass.c +6 -5
  45. data/ext/oj/rxclass.h +1 -1
  46. data/ext/oj/saj.c +10 -9
  47. data/ext/oj/saj2.c +37 -49
  48. data/ext/oj/saj2.h +1 -1
  49. data/ext/oj/scp.c +3 -14
  50. data/ext/oj/sparse.c +22 -70
  51. data/ext/oj/stream_writer.c +45 -41
  52. data/ext/oj/strict.c +20 -52
  53. data/ext/oj/string_writer.c +64 -38
  54. data/ext/oj/trace.h +31 -4
  55. data/ext/oj/usual.c +125 -114
  56. data/ext/oj/usual.h +7 -6
  57. data/ext/oj/util.h +1 -1
  58. data/ext/oj/val_stack.c +13 -2
  59. data/ext/oj/val_stack.h +8 -7
  60. data/ext/oj/wab.c +25 -57
  61. data/lib/oj/active_support_helper.rb +1 -3
  62. data/lib/oj/bag.rb +7 -1
  63. data/lib/oj/easy_hash.rb +4 -5
  64. data/lib/oj/error.rb +0 -1
  65. data/lib/oj/json.rb +162 -150
  66. data/lib/oj/mimic.rb +7 -7
  67. data/lib/oj/schandler.rb +5 -4
  68. data/lib/oj/state.rb +8 -5
  69. data/lib/oj/version.rb +1 -2
  70. data/lib/oj.rb +2 -0
  71. data/pages/InstallOptions.md +20 -0
  72. data/pages/Options.md +4 -0
  73. data/test/_test_active.rb +8 -9
  74. data/test/_test_active_mimic.rb +7 -8
  75. data/test/_test_mimic_rails.rb +17 -20
  76. data/test/activerecord/result_test.rb +5 -6
  77. data/test/activesupport6/encoding_test.rb +63 -28
  78. data/test/activesupport7/abstract_unit.rb +4 -1
  79. data/test/activesupport7/encoding_test.rb +72 -22
  80. data/test/files.rb +15 -15
  81. data/test/foo.rb +18 -69
  82. data/test/helper.rb +5 -8
  83. data/test/isolated/shared.rb +3 -2
  84. data/test/json_gem/json_addition_test.rb +2 -2
  85. data/test/json_gem/json_common_interface_test.rb +8 -6
  86. data/test/json_gem/json_encoding_test.rb +0 -0
  87. data/test/json_gem/json_ext_parser_test.rb +1 -0
  88. data/test/json_gem/json_fixtures_test.rb +3 -2
  89. data/test/json_gem/json_generator_test.rb +50 -33
  90. data/test/json_gem/json_generic_object_test.rb +11 -11
  91. data/test/json_gem/json_parser_test.rb +46 -46
  92. data/test/json_gem/json_string_matching_test.rb +9 -9
  93. data/test/mem.rb +13 -12
  94. data/test/perf.rb +21 -26
  95. data/test/perf_compat.rb +31 -33
  96. data/test/perf_dump.rb +28 -28
  97. data/test/perf_fast.rb +80 -82
  98. data/test/perf_file.rb +27 -29
  99. data/test/perf_object.rb +65 -69
  100. data/test/perf_once.rb +12 -11
  101. data/test/perf_parser.rb +42 -48
  102. data/test/perf_saj.rb +46 -54
  103. data/test/perf_scp.rb +57 -69
  104. data/test/perf_simple.rb +41 -39
  105. data/test/perf_strict.rb +68 -70
  106. data/test/perf_wab.rb +67 -69
  107. data/test/prec.rb +5 -5
  108. data/test/sample/change.rb +0 -1
  109. data/test/sample/dir.rb +0 -1
  110. data/test/sample/doc.rb +0 -1
  111. data/test/sample/file.rb +0 -1
  112. data/test/sample/group.rb +0 -1
  113. data/test/sample/hasprops.rb +0 -1
  114. data/test/sample/layer.rb +0 -1
  115. data/test/sample/rect.rb +0 -1
  116. data/test/sample/shape.rb +0 -1
  117. data/test/sample/text.rb +0 -1
  118. data/test/sample.rb +16 -16
  119. data/test/sample_json.rb +8 -8
  120. data/test/test_compat.rb +81 -54
  121. data/test/test_custom.rb +63 -52
  122. data/test/test_debian.rb +7 -10
  123. data/test/test_fast.rb +86 -90
  124. data/test/test_file.rb +24 -29
  125. data/test/test_gc.rb +5 -5
  126. data/test/test_generate.rb +5 -5
  127. data/test/test_hash.rb +4 -4
  128. data/test/test_integer_range.rb +9 -9
  129. data/test/test_null.rb +20 -20
  130. data/test/test_object.rb +92 -87
  131. data/test/test_parser.rb +4 -4
  132. data/test/test_parser_debug.rb +5 -5
  133. data/test/test_parser_saj.rb +27 -25
  134. data/test/test_parser_usual.rb +44 -6
  135. data/test/test_rails.rb +2 -2
  136. data/test/test_saj.rb +10 -8
  137. data/test/test_scp.rb +35 -35
  138. data/test/test_strict.rb +38 -32
  139. data/test/test_various.rb +146 -97
  140. data/test/test_wab.rb +46 -44
  141. data/test/test_writer.rb +63 -47
  142. data/test/tests.rb +7 -7
  143. data/test/tests_mimic.rb +6 -6
  144. data/test/tests_mimic_addition.rb +6 -6
  145. metadata +46 -26
  146. data/test/activesupport4/decoding_test.rb +0 -108
  147. data/test/activesupport4/encoding_test.rb +0 -531
  148. data/test/activesupport4/test_helper.rb +0 -41
  149. data/test/activesupport5/abstract_unit.rb +0 -45
  150. data/test/activesupport5/decoding_test.rb +0 -133
  151. data/test/activesupport5/encoding_test.rb +0 -500
  152. data/test/activesupport5/encoding_test_cases.rb +0 -98
  153. data/test/activesupport5/test_helper.rb +0 -72
  154. data/test/activesupport5/time_zone_test_helpers.rb +0 -39
  155. data/test/bar.rb +0 -11
  156. data/test/baz.rb +0 -16
  157. data/test/bug.rb +0 -16
  158. data/test/zoo.rb +0 -13
@@ -22,24 +22,24 @@ class JSONGeneratorTest < Test::Unit::TestCase
22
22
  }
23
23
  @json2 = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' +
24
24
  '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
25
- @json3 = <<'EOT'.chomp
26
- {
27
- "a": 2,
28
- "b": 3.141,
29
- "c": "c",
30
- "d": [
31
- 1,
32
- "b",
33
- 3.14
34
- ],
35
- "e": {
36
- "foo": "bar"
37
- },
38
- "g": "\"\u0000\u001f",
39
- "h": 1000.0,
40
- "i": 0.001
41
- }
42
- EOT
25
+ @json3 = <<~'EOT'.chomp
26
+ {
27
+ "a": 2,
28
+ "b": 3.141,
29
+ "c": "c",
30
+ "d": [
31
+ 1,
32
+ "b",
33
+ 3.14
34
+ ],
35
+ "e": {
36
+ "foo": "bar"
37
+ },
38
+ "g": "\"\u0000\u001f",
39
+ "h": 1000.0,
40
+ "i": 0.001
41
+ }
42
+ EOT
43
43
  end
44
44
 
45
45
  def test_generate
@@ -64,11 +64,11 @@ EOT
64
64
  parsed_json = JSON.parse(json)
65
65
  assert_equal(@hash, parsed_json)
66
66
  json = JSON.pretty_generate({1=>2})
67
- assert_equal(<<'EOT'.chomp, json)
68
- {
69
- "1": 2
70
- }
71
- EOT
67
+ assert_equal(<<~'EOT'.chomp, json)
68
+ {
69
+ "1": 2
70
+ }
71
+ EOT
72
72
  parsed_json = JSON.parse(json)
73
73
  assert_equal({"1"=>2}, parsed_json)
74
74
  assert_equal '666', JSON.pretty_generate(666)
@@ -78,15 +78,15 @@ EOT
78
78
 
79
79
  def test_generate_custom
80
80
  state = JSON::State.new(:space_before => " ", :space => " ", :indent => "<i>", :object_nl => "\n", :array_nl => "<a_nl>")
81
- json = JSON.generate({1=>{2=>3,4=>[5,6]}}, state)
82
- assert_equal(<<'EOT'.chomp, json)
83
- {
84
- <i>"1" : {
85
- <i><i>"2" : 3,
86
- <i><i>"4" : [<a_nl><i><i><i>5,<a_nl><i><i><i>6<a_nl><i><i>]
87
- <i>}
88
- }
89
- EOT
81
+ json = JSON.generate({1=>{2=>3, 4=>[5, 6]}}, state)
82
+ assert_equal(<<~'EOT'.chomp, json)
83
+ {
84
+ <i>"1" : {
85
+ <i><i>"2" : 3,
86
+ <i><i>"4" : [<a_nl><i><i><i>5,<a_nl><i><i><i>6<a_nl><i><i>]
87
+ <i>}
88
+ }
89
+ EOT
90
90
  end
91
91
 
92
92
  def test_fast_generate
@@ -142,6 +142,8 @@ EOT
142
142
  # seems to occur on travis but not locally.
143
143
  actual = state.to_h
144
144
  actual.delete(:escape_slash)
145
+ actual.delete(:strict)
146
+ actual.delete(:script_safe)
145
147
  assert_equal({
146
148
  :allow_nan => false,
147
149
  :array_nl => "\n",
@@ -162,6 +164,8 @@ EOT
162
164
  # seems to occur on travis but not locally.
163
165
  actual = state.to_h
164
166
  actual.delete(:escape_slash)
167
+ actual.delete(:strict)
168
+ actual.delete(:script_safe)
165
169
  assert_equal({
166
170
  :allow_nan => false,
167
171
  :array_nl => "",
@@ -182,6 +186,8 @@ EOT
182
186
  # seems to occur on travis but not locally.
183
187
  actual = state.to_h
184
188
  actual.delete(:escape_slash)
189
+ actual.delete(:strict)
190
+ actual.delete(:script_safe)
185
191
  assert_equal({
186
192
  :allow_nan => false,
187
193
  :array_nl => "",
@@ -352,7 +358,7 @@ EOT
352
358
  too_deep_ary = eval too_deep
353
359
  assert_raise(JSON::NestingError) { JSON.generate too_deep_ary }
354
360
  assert_raise(JSON::NestingError) { JSON.generate too_deep_ary, :max_nesting => 100 }
355
- ok = JSON.generate too_deep_ary, :max_nesting => 101
361
+ ok = JSON.generate too_deep_ary, :max_nesting => 102
356
362
  assert_equal too_deep, ok
357
363
  ok = JSON.generate too_deep_ary, :max_nesting => nil
358
364
  assert_equal too_deep, ok
@@ -393,4 +399,15 @@ EOT
393
399
  assert_equal '["foo"]', JSON.generate([s.new('foo')])
394
400
  end
395
401
  end
402
+
403
+ def test_invalid_to_json
404
+ omit if REAL_JSON_GEM
405
+
406
+ data = Object.new
407
+ def data.to_json(*)
408
+ nil
409
+ end
410
+
411
+ assert_raises(TypeError) { JSON.generate(data) }
412
+ end
396
413
  end
@@ -29,26 +29,26 @@ class JSONGenericObjectTest < Test::Unit::TestCase
29
29
 
30
30
  def test_parse_json
31
31
  x = JSON(
32
- '{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }',
33
- :create_additions => true
34
- )
35
- assert_kind_of Hash,
36
- JSON(
37
32
  '{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }',
38
33
  :create_additions => true
39
34
  )
35
+ assert_kind_of Hash,
36
+ JSON(
37
+ '{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }',
38
+ :create_additions => true
39
+ )
40
40
  switch_json_creatable do
41
41
  assert_equal @go, l =
42
- JSON(
43
- '{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }',
44
- :create_additions => true
45
- )
42
+ JSON(
43
+ '{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }',
44
+ :create_additions => true
45
+ )
46
46
  assert_equal 1, l.a
47
47
  assert_equal @go,
48
- l = JSON('{ "a": 1, "b": 2 }', :object_class => JSON::GenericObject)
48
+ l = JSON('{ "a": 1, "b": 2 }', :object_class => JSON::GenericObject)
49
49
  assert_equal 1, l.a
50
50
  assert_equal JSON::GenericObject[:a => JSON::GenericObject[:b => 2]],
51
- l = JSON('{ "a": { "b": 2 } }', :object_class => JSON::GenericObject)
51
+ l = JSON('{ "a": { "b": 2 } }', :object_class => JSON::GenericObject)
52
52
  assert_equal 2, l.a.b
53
53
  end
54
54
  end
@@ -95,21 +95,21 @@ class JSONParserTest < Test::Unit::TestCase
95
95
  assert_raise(JSON::ParserError) { JSON.parse('.23') }
96
96
  assert_raise(JSON::ParserError) { JSON.parse('023') }
97
97
  assert_equal 23, JSON.parse('23')
98
- assert_equal -23, JSON.parse('-23')
98
+ assert_equal(-23, JSON.parse('-23'))
99
99
  assert_equal_float 3.141, JSON.parse('3.141')
100
- assert_equal_float -3.141, JSON.parse('-3.141')
100
+ assert_equal_float(-3.141, JSON.parse('-3.141'))
101
101
  assert_equal_float 3.141, JSON.parse('3141e-3')
102
102
  assert_equal_float 3.141, JSON.parse('3141.1e-3')
103
103
  assert_equal_float 3.141, JSON.parse('3141E-3')
104
104
  assert_equal_float 3.141, JSON.parse('3141.0E-3')
105
- assert_equal_float -3.141, JSON.parse('-3141.0e-3')
106
- assert_equal_float -3.141, JSON.parse('-3141e-3')
105
+ assert_equal_float(-3.141, JSON.parse('-3141.0e-3'))
106
+ assert_equal_float(-3.141, JSON.parse('-3141e-3'))
107
107
  assert_raise(JSON::ParserError) { JSON.parse('NaN') }
108
108
  assert JSON.parse('NaN', :allow_nan => true).nan?
109
109
  assert_raise(JSON::ParserError) { JSON.parse('Infinity') }
110
110
  assert_equal 1.0/0, JSON.parse('Infinity', :allow_nan => true)
111
111
  assert_raise(JSON::ParserError) { JSON.parse('-Infinity') }
112
- assert_equal -1.0/0, JSON.parse('-Infinity', :allow_nan => true)
112
+ assert_equal(-1.0/0, JSON.parse('-Infinity', :allow_nan => true))
113
113
  end
114
114
 
115
115
  if Array.method_defined?(:permutation)
@@ -133,17 +133,17 @@ class JSONParserTest < Test::Unit::TestCase
133
133
  end
134
134
 
135
135
  def test_parse_arrays
136
- assert_equal([1,2,3], JSON.parse('[1,2,3]'))
137
- assert_equal([1.2,2,3], JSON.parse('[1.2,2,3]'))
138
- assert_equal([[],[[],[]]], JSON.parse('[[],[[],[]]]'))
136
+ assert_equal([1, 2, 3], JSON.parse('[1,2,3]'))
137
+ assert_equal([1.2, 2, 3], JSON.parse('[1.2,2,3]'))
138
+ assert_equal([[], [[], []]], JSON.parse('[[],[[],[]]]'))
139
139
  assert_equal([], JSON.parse('[]'))
140
140
  assert_equal([], JSON.parse(' [ ] '))
141
141
  assert_equal([1], JSON.parse('[1]'))
142
142
  assert_equal([1], JSON.parse(' [ 1 ] '))
143
143
  ary = [[1], ["foo"], [3.14], [4711.0], [2.718], [nil],
144
- [[1, -2, 3]], [false], [true]]
144
+ [[1, -2, 3]], [false], [true]]
145
145
  assert_equal(ary,
146
- JSON.parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]],[false],[true]]'))
146
+ JSON.parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]],[false],[true]]'))
147
147
  assert_equal(ary, JSON.parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2]\s
148
148
  , [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
149
149
  end
@@ -208,51 +208,51 @@ class JSONParserTest < Test::Unit::TestCase
208
208
 
209
209
  def test_symbolize_names
210
210
  assert_equal({ "foo" => "bar", "baz" => "quux" },
211
- JSON.parse('{"foo":"bar", "baz":"quux"}'))
211
+ JSON.parse('{"foo":"bar", "baz":"quux"}'))
212
212
  assert_equal({ :foo => "bar", :baz => "quux" },
213
- JSON.parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true))
213
+ JSON.parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true))
214
214
  assert_raise(ArgumentError) do
215
215
  JSON.parse('{}', :symbolize_names => true, :create_additions => true)
216
216
  end
217
217
  end
218
218
 
219
219
  def test_parse_comments
220
- json = <<EOT
221
- {
222
- "key1":"value1", // eol comment
223
- "key2":"value2" /* multi line
224
- * comment */,
225
- "key3":"value3" /* multi line
226
- // nested eol comment
227
- * comment */
228
- }
229
- EOT
220
+ json = <<~EOT
221
+ {
222
+ "key1":"value1", // eol comment
223
+ "key2":"value2" /* multi line
224
+ * comment */,
225
+ "key3":"value3" /* multi line
226
+ // nested eol comment
227
+ * comment */
228
+ }
229
+ EOT
230
230
  assert_equal(
231
231
  { "key1" => "value1", "key2" => "value2", "key3" => "value3" },
232
232
  JSON.parse(json))
233
- json = <<EOT
234
- {
235
- "key1":"value1" /* multi line
236
- // nested eol comment
237
- /* illegal nested multi line comment */
238
- * comment */
239
- }
240
- EOT
233
+ json = <<~EOT
234
+ {
235
+ "key1":"value1" /* multi line
236
+ // nested eol comment
237
+ /* illegal nested multi line comment */
238
+ * comment */
239
+ }
240
+ EOT
241
241
  assert_raise(JSON::ParserError) { JSON.parse(json) }
242
- json = <<EOT
243
- {
244
- "key1":"value1" /* multi line
245
- // nested eol comment
246
- closed multi comment */
247
- and again, throw an Error */
248
- }
249
- EOT
242
+ json = <<~EOT
243
+ {
244
+ "key1":"value1" /* multi line
245
+ // nested eol comment
246
+ closed multi comment */
247
+ and again, throw an Error */
248
+ }
249
+ EOT
250
250
  assert_raise(JSON::ParserError) { JSON.parse(json) }
251
- json = <<EOT
252
- {
253
- "key1":"value1" /*/*/
254
- }
255
- EOT
251
+ json = <<~EOT
252
+ {
253
+ "key1":"value1" /*/*/
254
+ }
255
+ EOT
256
256
  assert_equal({ "key1" => "value1" }, JSON.parse(json))
257
257
  end
258
258
 
@@ -348,7 +348,7 @@ EOT
348
348
 
349
349
  def test_parse_array_custom_array_derived_class
350
350
  res = JSON.parse('[1,2]', :array_class => SubArray)
351
- assert_equal([1,2], res)
351
+ assert_equal([1, 2], res)
352
352
  assert_equal(SubArray, res.class)
353
353
  assert res.shifted?
354
354
  end
@@ -356,7 +356,7 @@ EOT
356
356
  def test_parse_array_custom_non_array_derived_class
357
357
  res = JSON.parse('[1,2]', :array_class => SubArrayWrapper)
358
358
  assert_equal(SubArrayWrapper, res.class)
359
- assert_equal([1,2], res.data)
359
+ assert_equal([1, 2], res.data)
360
360
  assert res.shifted?
361
361
  end
362
362
 
@@ -442,7 +442,7 @@ EOT
442
442
  assert obj_again['foo']['bar']
443
443
  assert_equal obj, obj_again
444
444
  assert_equal ["foo"],
445
- JSON(JSON(SubArray2["foo"]), :create_additions => true)
445
+ JSON(JSON(SubArray2["foo"]), :create_additions => true)
446
446
  end
447
447
 
448
448
  def test_generate_core_subclasses_with_default_to_json
@@ -28,15 +28,15 @@ class JSONStringMatchingTest < Test::Unit::TestCase
28
28
  t_json = [ t ].to_json
29
29
  time_regexp = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\z/
30
30
  assert_equal [ t ],
31
- JSON.parse(
32
- t_json,
33
- :create_additions => true,
34
- :match_string => { time_regexp => TestTime }
35
- )
31
+ JSON.parse(
32
+ t_json,
33
+ :create_additions => true,
34
+ :match_string => { time_regexp => TestTime }
35
+ )
36
36
  assert_equal [ t.strftime('%FT%T%z') ],
37
- JSON.parse(
38
- t_json,
39
- :match_string => { time_regexp => TestTime }
40
- )
37
+ JSON.parse(
38
+ t_json,
39
+ :match_string => { time_regexp => TestTime }
40
+ )
41
41
  end
42
42
  end
data/test/mem.rb CHANGED
@@ -1,29 +1,30 @@
1
1
  #!/usr/bin/env ruby
2
- # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
- $: << '.'
5
- $: << File.join(File.dirname(__FILE__), "../lib")
6
- $: << File.join(File.dirname(__FILE__), "../ext")
4
+ require 'English'
5
+ $LOAD_PATH << '.'
6
+ $LOAD_PATH << File.join(__dir__, '../lib')
7
+ $LOAD_PATH << File.join(__dir__, '../ext')
7
8
 
8
9
  require 'oj'
9
10
 
10
11
  Oj.default_options = { mode: :rails, cache_keys: false, cache_str: -1 }
11
12
 
12
13
  def mem
13
- `ps -o rss= -p #{$$}`.to_i
14
+ `ps -o rss= -p #{$PROCESS_ID}`.to_i
14
15
  end
15
16
 
16
17
  ('a'..'z').each { |a|
17
18
  ('a'..'z').each { |b|
18
19
  ('a'..'z').each { |c|
19
20
  ('a'..'z').each { |d|
20
- ('a'..'z').each { |e|
21
- ('a'..'z').each { |f|
22
- key = "#{a}#{b}#{c}#{d}#{e}#{f}"
23
- x = Oj.load(%|{ "#{key}": 101}|)
24
- #Oj.dump(x)
25
- }
26
- }
21
+ ('a'..'z').each { |e|
22
+ ('a'..'z').each { |f|
23
+ key = "#{a}#{b}#{c}#{d}#{e}#{f}"
24
+ Oj.load(%|{ "#{key}": 101}|)
25
+ # Oj.dump(x)
26
+ }
27
+ }
27
28
  }
28
29
  }
29
30
  puts "#{a}#{b} #{mem}"
data/test/perf.rb CHANGED
@@ -1,7 +1,8 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  class Perf
3
4
 
4
- def initialize()
5
+ def initialize
5
6
  @items = []
6
7
  end
7
8
 
@@ -19,7 +20,7 @@ class Perf
19
20
  end
20
21
 
21
22
  def run(iter)
22
- base = Item.new(nil, nil) { }
23
+ base = Item.new(nil, nil) {}
23
24
  base.run(iter, 0.0)
24
25
  @items.each do |i|
25
26
  i.run(iter, base.duration)
@@ -32,35 +33,31 @@ class Perf
32
33
  summary()
33
34
  end
34
35
 
35
- def summary()
36
- fastest = nil
37
- slowest = nil
36
+ def summary
38
37
  width = 6
39
38
  @items.each do |i|
40
39
  next if i.duration.nil?
40
+
41
41
  width = i.title.size if width < i.title.size
42
42
  end
43
43
  iva = @items.clone
44
44
  iva.delete_if { |i| i.duration.nil? }
45
45
  iva = iva.sort_by { |i| i.duration }
46
46
  puts
47
- puts "Summary:"
48
- puts "%*s time (secs) rate (ops/sec)" % [width, 'System']
47
+ puts 'Summary:'
48
+ puts '%*s time (secs) rate (ops/sec)' % [width, 'System']
49
49
  puts "#{'-' * width} ----------- --------------"
50
50
  iva.each do |i|
51
- if i.duration.nil?
52
- else
53
- puts "%*s %11.3f %14.3f" % [width, i.title, i.duration, i.rate ]
54
- end
51
+ puts '%*s %11.3f %14.3f' % [width, i.title, i.duration, i.rate ] unless i.duration.nil?
55
52
  end
56
53
  puts
57
54
  puts "Comparison Matrix\n(performance factor, 2.0 means row is twice as fast as column)"
58
- puts ([' ' * width] + iva.map { |i| "%*s" % [width, i.title] }).join(' ')
59
- puts (['-' * width] + iva.map { |i| '-' * width }).join(' ')
55
+ puts ([' ' * width] + iva.map { |i| '%*s' % [width, i.title] }).join(' ')
56
+ puts (['-' * width] + iva.map { |_i| '-' * width }).join(' ')
60
57
  iva.each do |i|
61
- line = ["%*s" % [width, i.title]]
58
+ line = ['%*s' % [width, i.title]]
62
59
  iva.each do |o|
63
- line << "%*.2f" % [width, o.duration / i.duration]
60
+ line << ('%*.2f' % [width, o.duration / i.duration])
64
61
  end
65
62
  puts line.join(' ')
66
63
  end
@@ -90,17 +87,15 @@ class Perf
90
87
  end
91
88
 
92
89
  def run(iter, base)
93
- begin
94
- GC.start
95
- @before.call unless @before.nil?
96
- start = Time.now
97
- iter.times { @blk.call }
98
- @duration = Time.now - start - base
99
- @duration = 0.0 if @duration < 0.0
100
- @rate = iter / @duration
101
- rescue Exception => e
102
- @error = "#{e.class}: #{e.message}"
103
- end
90
+ GC.start
91
+ @before.call unless @before.nil?
92
+ start = Time.now
93
+ iter.times { @blk.call }
94
+ @duration = Time.now - start - base
95
+ @duration = 0.0 if @duration < 0.0
96
+ @rate = iter / @duration
97
+ rescue Exception => e
98
+ @error = "#{e.class}: #{e.message}"
104
99
  end
105
100
 
106
101
  end # Item
data/test/perf_compat.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
- # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
- $: << '.'
5
- $: << File.join(File.dirname(__FILE__), "../lib")
6
- $: << File.join(File.dirname(__FILE__), "../ext")
4
+ $LOAD_PATH << '.'
5
+ $LOAD_PATH << File.join(__dir__, '../lib')
6
+ $LOAD_PATH << File.join(__dir__, '../ext')
7
7
 
8
8
  require 'optparse'
9
9
  require 'perf'
@@ -11,25 +11,23 @@ require 'oj'
11
11
 
12
12
  $verbose = false
13
13
  $indent = 0
14
- $iter = 20000
14
+ $iter = 20_000
15
15
  $size = 0
16
16
 
17
17
  opts = OptionParser.new
18
- opts.on("-v", "verbose") { $verbose = true }
19
- opts.on("-c", "--count [Int]", Integer, "iterations") { |i| $iter = i }
20
- opts.on("-i", "--indent [Int]", Integer, "indentation") { |i| $indent = i }
21
- opts.on("-s", "--size [Int]", Integer, "size (~Kbytes)") { |i| $size = i }
22
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
23
- files = opts.parse(ARGV)
18
+ opts.on('-v', 'verbose') { $verbose = true }
19
+ opts.on('-c', '--count [Int]', Integer, 'iterations') { |i| $iter = i }
20
+ opts.on('-i', '--indent [Int]', Integer, 'indentation') { |i| $indent = i }
21
+ opts.on('-s', '--size [Int]', Integer, 'size (~Kbytes)') { |i| $size = i }
22
+ opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
23
+ opts.parse(ARGV)
24
24
 
25
25
  def capture_error(tag, orig, load_key, dump_key, &blk)
26
- begin
27
- obj = blk.call(orig)
28
- puts obj unless orig == obj
29
- raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
30
- rescue Exception => e
31
- $failed[tag] = "#{e.class}: #{e.message}"
32
- end
26
+ obj = blk.call(orig)
27
+ puts obj unless orig == obj
28
+ raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
29
+ rescue Exception => e
30
+ $failed[tag] = "#{e.class}: #{e.message}"
33
31
  end
34
32
 
35
33
  # Verify that all packages dump and load correctly and return the same Object as the original.
@@ -39,7 +37,7 @@ capture_error('JSON::Ext', $obj, 'generate', 'parse') { |o|
39
37
  require 'json/ext'
40
38
  JSON.generator = JSON::Ext::Generator
41
39
  JSON.parser = JSON::Ext::Parser
42
- JSON.load(JSON.generate(o))
40
+ JSON.parse(JSON.generate(o))
43
41
  }
44
42
 
45
43
  module One
@@ -47,7 +45,7 @@ module One
47
45
  module Three
48
46
  class Empty
49
47
 
50
- def initialize()
48
+ def initialize
51
49
  @a = 1
52
50
  @b = 2
53
51
  @c = 3
@@ -58,16 +56,16 @@ module One
58
56
  end
59
57
  alias == eql?
60
58
 
61
- def as_json(*a)
59
+ def as_json(*_a)
62
60
  {JSON.create_id => self.class.name, 'a' => @a, 'b' => @b, 'c' => @c }
63
61
  end
64
-
65
- def to_json(*a)
62
+
63
+ def to_json(*_a)
66
64
  JSON.generate(as_json())
67
65
  end
68
66
 
69
- def self.json_create(h)
70
- self.new()
67
+ def self.json_create(_h)
68
+ new()
71
69
  end
72
70
  end # Empty
73
71
  end # Three
@@ -77,8 +75,8 @@ end # One
77
75
  $obj = {
78
76
  'a' => 'Alpha', # string
79
77
  'b' => true, # boolean
80
- 'c' => 12345, # number
81
- 'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
78
+ 'c' => 12_345, # number
79
+ 'd' => [ true, [false, [-123_456_789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
82
80
  'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
83
81
  'f' => nil, # nil
84
82
  'g' => One::Two::Three::Empty.new(),
@@ -109,13 +107,13 @@ if $verbose
109
107
  end
110
108
 
111
109
  puts '-' * 80
112
- puts "Compat Parse Performance"
110
+ puts 'Compat Parse Performance'
113
111
  perf = Perf.new()
114
- unless $failed.has_key?('JSON::Ext')
115
- perf.add('JSON::Ext', 'parse') { JSON.load($json) }
112
+ unless $failed.key?('JSON::Ext')
113
+ perf.add('JSON::Ext', 'parse') { JSON.parse($json) }
116
114
  perf.before('JSON::Ext') { JSON.parser = JSON::Ext::Parser }
117
115
  end
118
- unless $failed.has_key?('Oj:compat')
116
+ unless $failed.key?('Oj:compat')
119
117
  perf.add('Oj:compat', 'compat_load') { Oj.compat_load($json) }
120
118
  end
121
119
  perf.run($iter)
@@ -125,6 +123,6 @@ puts '-' * 80
125
123
  puts
126
124
 
127
125
  unless $failed.empty?
128
- puts "The following packages were not included for the reason listed"
129
- $failed.each { |tag,msg| puts "***** #{tag}: #{msg}" }
126
+ puts 'The following packages were not included for the reason listed'
127
+ $failed.each { |tag, msg| puts "***** #{tag}: #{msg}" }
130
128
  end