<commit_before>require File.dirname(__FILE__) + '/helper.rb'


class TestRound < Test::Unit::TestCase


  def setup
    initialize_context
  end


  def test_round

    assert_equal(101,  Decimal('100.5').round)
    assert Decimal('100.5').round.kind_of?(Integer)
    assert_equal 100, Decimal('100.4999999999').round
    assert_equal(-101, Decimal('-100.5').round)
    assert_equal(-100, Decimal('-100.4999999999').round)

    assert_equal 101, Decimal('100.5').round(:places=>0)
    assert Decimal('100.5').round(:places=>0).kind_of?(Decimal)
    assert_equal 101, Decimal('100.5').round(0)
    assert Decimal('100.5').round(0).kind_of?(Decimal)

    assert_equal Decimal('123.12'), Decimal('123.123').round(2)
    assert_equal Decimal('123'), Decimal('123.123').round(0)
    assert_equal Decimal('120'), Decimal('123.123').round(-1)
    assert_equal Decimal('120'), Decimal('123.123').round(:precision=>2)
    assert_equal Decimal('123.12'), Decimal('123.123').round(:precision=>5)

    assert_equal 100, Decimal('100.5').round(:rounding=>:half_even)
    assert_equal 101, Decimal('100.5000001').round(:rounding=>:half_even)
    assert_equal 102, Decimal('101.5').round(:rounding=>:half_even)
    assert_equal 101, Decimal('101.4999999999').round(:rounding=>:half_even)

    assert_equal 101, Decimal('100.0001').ceil
    assert_equal(-100, Decimal('-100.0001').ceil)
    assert_equal(-100, Decimal('-100.9999').ceil)
    assert_equal 100, Decimal('100.9999').floor
    assert_equal(-101, Decimal('-100.9999').floor)
    assert_equal(-101, Decimal('-100.0001').floor)

    assert_equal 100, Decimal('100.99999').truncate
    assert_equal(-100, Decimal('-100.99999').truncate)

  end


end<commit_msg>Add round test that uses rounding mode detection function.<commit_after>require File.dirname(__FILE__) + '/helper.rb'


class TestRound < Test::Unit::TestCase


  def setup
    initialize_context
  end


  def test_round

    assert_equal(101,  Decimal('100.5').round)
    assert Decimal('100.5').round.kind_of?(Integer)
    assert_equal 100, Decimal('100.4999999999').round
    assert_equal(-101, Decimal('-100.5').round)
    assert_equal(-100, Decimal('-100.4999999999').round)

    assert_equal 101, Decimal('100.5').round(:places=>0)
    assert Decimal('100.5').round(:places=>0).kind_of?(Decimal)
    assert_equal 101, Decimal('100.5').round(0)
    assert Decimal('100.5').round(0).kind_of?(Decimal)

    assert_equal Decimal('123.12'), Decimal('123.123').round(2)
    assert_equal Decimal('123'), Decimal('123.123').round(0)
    assert_equal Decimal('120'), Decimal('123.123').round(-1)
    assert_equal Decimal('120'), Decimal('123.123').round(:precision=>2)
    assert_equal Decimal('123.12'), Decimal('123.123').round(:precision=>5)

    assert_equal 100, Decimal('100.5').round(:rounding=>:half_even)
    assert_equal 101, Decimal('100.5000001').round(:rounding=>:half_even)
    assert_equal 102, Decimal('101.5').round(:rounding=>:half_even)
    assert_equal 101, Decimal('101.4999999999').round(:rounding=>:half_even)

    assert_equal 101, Decimal('100.0001').ceil
    assert_equal(-100, Decimal('-100.0001').ceil)
    assert_equal(-100, Decimal('-100.9999').ceil)
    assert_equal 100, Decimal('100.9999').floor
    assert_equal(-101, Decimal('-100.9999').floor)
    assert_equal(-101, Decimal('-100.0001').floor)

    assert_equal 100, Decimal('100.99999').truncate
    assert_equal(-100, Decimal('-100.99999').truncate)

  end

  def detect_rounding(type)
    x = type.new(1)*type.int_radix_power(type.context.precision+1)
    y = x + type.int_radix_power(2)
    h = type.radix/2
    b = h*type.radix
    z = type.new(type.int_radix_power(2) - 1)
    type.context do
      type.context.precision = 8 if type.context.exact?
      if x + 1 == y
        if (y + 1 == y) && type.radix==10
          :up05
        elsif -x - 1 == -y
          :up
        else
          :ceiling
        end
      else # x + 1 == x
        if x + z == x
          if -x - z == -x
            :down
          else
            :floor
          end
        else # x + z == y
          # round to nearest
          if x + b == x
            if y + b == y
              :half_down
            else
              :half_even
            end
          else # x + b == y
            :half_up
          end
        end
      end
    end
  end

  def test_round_detection
    [Decimal, BinFloat].each do |type|
      [:half_even, :half_up, :half_down, :down, :up, :floor, :ceiling, :up05].each do |rounding|
        type.context(:rounding=>rounding) do
          next if type==BinFloat && rounding==:up05
          assert_equal rounding, detect_rounding(type)
        end
      end
    end
  end


end
{'fraction_non_alphanumeric': 0.16279069767441862, 'fraction_numerical': 0.11286537230637934, 'mean_word_length': 3.1340388007054676, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5857220', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1576, 'n_words': 281}