Sha256: f85d8bdd95cfbd3670da48dfd5196b3d928ce4c9b26c8513acd0fea3bc199795

Contents?: true

Size: 1.21 KB

Versions: 23

Compression:

Stored size: 1.21 KB

Contents

require "spec_helper"

describe Stellar::Price, "#from_f" do
  subject{ Stellar::Price }
  let(:seed){ 225571644875421139403973254661022579608 } #generated using Random.new
  let(:random){ Random.new(seed) } 
  let(:iterations){ ENV["SMOKE_ITERATIONS"].present? ? ENV["SMOKE_ITERATIONS"].to_i : 2000}

  it "withstands a random smoke test" do
    iterations.times do |i|
      expected = random.rand
      actual_p = subject.from_f(expected)
      actual = actual_p.to_f

      expect(actual).to be_within(0.000000001).of(actual)
      expect(actual_p.n).to be <= Stellar::Price::MAX_PRECISION
      expect(actual_p.d).to be <= Stellar::Price::MAX_PRECISION
    end
  end

  it "works with bigdecimal" do
      whole      = random.rand(1_000_000)
      fractional = random.rand(10_000_000) # seven significant digits available for fractional
      
      expected = BigDecimal.new("#{whole}.#{fractional}")
      actual_p = subject.from_f(expected)
      actual = BigDecimal.new(actual_p.n) / BigDecimal.new(actual_p.d) 

      expect(actual).to be_within(BigDecimal.new("0.000000001")).of(actual)
      expect(actual_p.n).to be <= Stellar::Price::MAX_PRECISION
      expect(actual_p.d).to be <= Stellar::Price::MAX_PRECISION
  end

end

Version data entries

23 entries across 23 versions & 3 rubygems

Version Path
stellar-base-0.19.0 spec/lib/stellar/price_spec.rb
stellar-base-0.18.0 spec/lib/stellar/price_spec.rb
stellar-base-0.17.0 spec/lib/stellar/price_spec.rb
stellar-base-0.16.0 spec/lib/stellar/price_spec.rb
stellar-base-0.15.0 spec/lib/stellar/price_spec.rb
stellar-base-0.14.0 spec/lib/stellar/price_spec.rb
stellar-base-0.13.0 spec/lib/stellar/price_spec.rb
stellar-base-0.12.0 spec/lib/stellar/price_spec.rb
stellar-base-0.11.0 spec/lib/stellar/price_spec.rb
auction-ruby-base-0.1.3 spec/lib/stellar/price_spec.rb
auction-ruby-base-0.1.2 spec/lib/stellar/price_spec.rb
auction-ruby-base-0.1.1 spec/lib/stellar/price_spec.rb
stellar-base-0.10.0 spec/lib/stellar/price_spec.rb
stellar-base-0.9.0 spec/lib/stellar/price_spec.rb
stellar-base-0.8.0 spec/lib/stellar/price_spec.rb
stellar-base-0.7.0 spec/lib/stellar/price_spec.rb
stellar-base-0.6.1 spec/lib/stellar/price_spec.rb
stellar-base-0.6.0 spec/lib/stellar/price_spec.rb
stellar-base-0.5.0 spec/lib/stellar/price_spec.rb
stellar-base-0.4.0 spec/lib/stellar/price_spec.rb