defmodule RustlerBigintTest.BigIntTest do
  use ExUnit.Case, async: true

  # using explicit large numbers, because the `**/2` function was recently added in 1.13
  # so older elixir version don't support this

  test "echo" do
    # 3**1234
    large_number =
      5_856_367_529_932_071_269_049_608_726_415_028_439_757_143_623_443_063_785_468_784_843_536_946_634_255_003_858_796_842_453_459_905_186_846_046_851_774_292_445_753_149_532_138_648_409_140_561_593_823_222_428_559_200_283_816_367_593_571_556_942_770_711_203_894_947_318_922_509_986_128_995_034_233_177_140_247_221_695_941_679_146_171_215_675_158_154_358_685_224_309_073_135_481_486_540_427_499_644_363_484_815_218_991_488_230_440_687_359_021_965_115_067_678_545_212_365_725_678_242_878_485_920_885_436_927_030_372_580_147_256_775_788_882_374_301_866_845_441_065_076_726_499_613_479_868_210_410_548_366_056_401_445_912_760_529_623_658_838_510_267_592_199_677_099_676_046_090_140_514_526_228_797_525_926_977_514_932_394_954_796_457_727_416_398_081_482_661_262_807_288_229_389_463_819_882_569

    assert RustlerBigintTest.echo(large_number) == large_number
  end

  test "echo zero" do
    assert RustlerBigintTest.echo(0) == 0
  end

  test "add one large big integer" do
    # 2**2048 - 1
    large_number =
      32_317_006_071_311_007_300_714_876_688_669_951_960_444_102_669_715_484_032_130_345_427_524_655_138_867_890_893_197_201_411_522_913_463_688_717_960_921_898_019_494_119_559_150_490_921_095_088_152_386_448_283_120_630_877_367_300_996_091_750_197_750_389_652_106_796_057_638_384_067_568_276_792_218_642_619_756_161_838_094_338_476_170_470_581_645_852_036_305_042_887_575_891_541_065_808_607_552_399_123_930_385_521_914_333_389_668_342_420_684_974_786_564_569_494_856_176_035_326_322_058_077_805_659_331_026_192_708_460_314_150_258_592_864_177_116_725_943_603_718_461_857_357_598_351_152_301_645_904_403_697_613_233_287_231_227_125_684_710_820_209_725_157_101_726_931_323_469_678_542_580_656_697_935_045_997_268_352_998_638_215_525_166_389_437_335_543_602_135_433_229_604_645_318_478_604_952_148_193_555_853_611_059_596_230_655

    assert RustlerBigintTest.add_one(large_number) == large_number + 1
    assert RustlerBigintTest.add_one(-large_number) == -large_number + 1
  end

  test "add one small big integer" do
    # 2**128 - 1
    large_number = 340_282_366_920_938_463_463_374_607_431_768_211_455

    assert RustlerBigintTest.add_one(large_number) == large_number + 1
    assert RustlerBigintTest.add_one(-large_number) == -large_number + 1
  end

  test "add one to small integer" do
    assert RustlerBigintTest.add_one(1) == 2
  end

  test "add one to negative integer" do
    assert RustlerBigintTest.add_one(-2) == -1
  end

  test "add one to normal integer" do
    assert RustlerBigintTest.add_one(512) == 513
  end

  test "add" do
    # 3**1234
    large_number_a =
      5_856_367_529_932_071_269_049_608_726_415_028_439_757_143_623_443_063_785_468_784_843_536_946_634_255_003_858_796_842_453_459_905_186_846_046_851_774_292_445_753_149_532_138_648_409_140_561_593_823_222_428_559_200_283_816_367_593_571_556_942_770_711_203_894_947_318_922_509_986_128_995_034_233_177_140_247_221_695_941_679_146_171_215_675_158_154_358_685_224_309_073_135_481_486_540_427_499_644_363_484_815_218_991_488_230_440_687_359_021_965_115_067_678_545_212_365_725_678_242_878_485_920_885_436_927_030_372_580_147_256_775_788_882_374_301_866_845_441_065_076_726_499_613_479_868_210_410_548_366_056_401_445_912_760_529_623_658_838_510_267_592_199_677_099_676_046_090_140_514_526_228_797_525_926_977_514_932_394_954_796_457_727_416_398_081_482_661_262_807_288_229_389_463_819_882_569

    large_number_b =
      681_983_153_251_908_768_114_683_169_927_987_070_668_660_497_285_194_786_826_348_012_081_194_090_502_408_609_050_220_606_330_828_939_572_934_199_362_864_938_391_514_279_602_520_887_342_125_832_131_103_023_939_491_393_422_681_125_665_930_454_994_987_766_963_689_143_999_669_261_833_417_460_430_651_104_622_296_909_607_016_697_321_698_668_920_735_361_575_668_182_639_826_569_926_362_197_863_024_816_155_985_798_528_825_030_495_491_283_046_708_347_408_809_497_706_504_810_195_944_911_488_979_153_363_882_327_857_715_398_511_164_074_473_944_583_368_777_257_040_538_854_918_875_528_258_317_398_203_813_372_819_434_441_249_985_124_662_289_813_821_939_004_848_491_608_424_088_502_418_768_761_684_288_045_334_237_380_008_325_664_276_021_445_264_763_798_377_316_561_527_550_220_489_501_953_125

    assert RustlerBigintTest.add(large_number_a, large_number_b) ==
             large_number_a + large_number_b
  end
end
