tc_speed.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/local/bin/ruby -w
  2. # tc_speed.rb
  3. #
  4. # Created by James Edward Gray II on 2005-11-14.
  5. # Copyright 2012 Gray Productions. All rights reserved.
  6. require "test/unit"
  7. require "timeout"
  8. require "faster_csv"
  9. require "csv"
  10. class TestFasterCSVSpeed < Test::Unit::TestCase
  11. PATH = File.join(File.dirname(__FILE__), "test_data.csv")
  12. BIG_DATA = "123456789\n" * 1024
  13. def test_that_we_are_doing_the_same_work
  14. FasterCSV.open(PATH) do |csv|
  15. CSV.foreach(PATH) do |row|
  16. assert_equal(row, csv.shift)
  17. end
  18. end
  19. end
  20. def test_speed_vs_csv
  21. csv_time = Time.now
  22. CSV.foreach(PATH) do |row|
  23. # do nothing, we're just timing a read...
  24. end
  25. csv_time = Time.now - csv_time
  26. faster_csv_time = Time.now
  27. FasterCSV.foreach(PATH) do |row|
  28. # do nothing, we're just timing a read...
  29. end
  30. faster_csv_time = Time.now - faster_csv_time
  31. assert(faster_csv_time < csv_time / 3)
  32. end
  33. def test_the_parse_fails_fast_when_it_can_for_unquoted_fields
  34. assert_parse_errors_out('valid,fields,bad start"' + BIG_DATA)
  35. end
  36. def test_the_parse_fails_fast_when_it_can_for_unescaped_quotes
  37. assert_parse_errors_out('valid,fields,"bad start"unescaped' + BIG_DATA)
  38. end
  39. def test_field_size_limit_controls_lookahead
  40. assert_parse_errors_out( 'valid,fields,"' + BIG_DATA + '"',
  41. :field_size_limit => 2048 )
  42. end
  43. private
  44. def assert_parse_errors_out(*args)
  45. assert_raise(FasterCSV::MalformedCSVError) do
  46. Timeout.timeout(0.2) do
  47. FasterCSV.parse(*args)
  48. fail("Parse didn't error out")
  49. end
  50. end
  51. end
  52. end