Sha256: 5199227809695f9536d38152a192649774d3c1be02cad63ec4486e4e7137e2c9

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

#!/usr/local/bin/ruby -w

# tc_speed.rb
#
#  Created by James Edward Gray II on 2005-11-14.
#  Copyright 2012 Gray Productions. All rights reserved.

require "test/unit"
require "timeout"

require "faster_csv"
require "csv"

class TestFasterCSVSpeed < Test::Unit::TestCase
  PATH     = File.join(File.dirname(__FILE__), "test_data.csv")
  BIG_DATA = "123456789\n" * 1024
  
  def test_that_we_are_doing_the_same_work
    FasterCSV.open(PATH) do |csv|
      CSV.foreach(PATH) do |row|
        assert_equal(row, csv.shift)
      end
    end
  end
  
  def test_speed_vs_csv
    csv_time = Time.now
    CSV.foreach(PATH) do |row|
      # do nothing, we're just timing a read...
    end
    csv_time = Time.now - csv_time

    faster_csv_time = Time.now
    FasterCSV.foreach(PATH) do |row|
      # do nothing, we're just timing a read...
    end
    faster_csv_time = Time.now - faster_csv_time
    
    assert(faster_csv_time < csv_time / 3)
  end
  
  def test_the_parse_fails_fast_when_it_can_for_unquoted_fields
    assert_parse_errors_out('valid,fields,bad start"' + BIG_DATA)
  end
  
  def test_the_parse_fails_fast_when_it_can_for_unescaped_quotes
    assert_parse_errors_out('valid,fields,"bad start"unescaped' + BIG_DATA)
  end
  
  def test_field_size_limit_controls_lookahead
    assert_parse_errors_out( 'valid,fields,"' + BIG_DATA + '"',
                             :field_size_limit => 2048 )
  end
  
  private
  
  def assert_parse_errors_out(*args)
    assert_raise(FasterCSV::MalformedCSVError) do
      Timeout.timeout(0.2) do
        FasterCSV.parse(*args)
        fail("Parse didn't error out")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
asana2flowdock-1.0.0 vendor/bundle/ruby/1.9.1/gems/fastercsv-1.5.5/test/tc_speed.rb
fastercsv-1.5.5 test/tc_speed.rb