Skip to content

Commit

Permalink
Add method for integer and float types
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Laureano committed Nov 23, 2024
1 parent 286a757 commit a00859a
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 0 deletions.
144 changes: 144 additions & 0 deletions spec/chaos_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,161 @@ describe Chaos do
end

# chaos return with Integer int8
it "chaos - int8: returns a correct value" do
chaos = Chaos::Chaos.new
chaos.probability = 0.0
chaos.chaos(8_i8).should eq(8_i8)
end

it "chaos - int8: returns a chaotic value" do
chaos = Chaos::Chaos.new
chaos.probability = 1.0
chaos.chaos(8_i8).should_not eq(8_i8)
end

# chaos return with Integer int16
it "chaos - int16: returns a correct value" do
chaos = Chaos::Chaos.new
chaos.probability = 0.0
chaos.chaos(16_i16).should eq(16_i16)
end

it "chaos - int16: returns a chaotic value" do
chaos = Chaos::Chaos.new
chaos.probability = 1.0
chaos.chaos(16_i16).should_not eq(16_i16)
end

# chaos return with Integer int32
it "chaos - int32: returns a correct value" do
chaos = Chaos::Chaos.new
chaos.probability = 0.0
chaos.chaos(32_i32).should eq(32_i32)
end

it "chaos - int32: returns a chaotic value" do
chaos = Chaos::Chaos.new
chaos.probability = 1.0
chaos.chaos(32_i32).should_not eq(32_i32)
end

# chaos return with Integer int64
it "chaos - int64: returns a correct value" do
chaos = Chaos::Chaos.new
chaos.probability = 0.0
chaos.chaos(64_i64).should eq(64_i64)
end

it "chaos - int64: returns a chaotic value" do
chaos = Chaos::Chaos.new
chaos.probability = 1.0
chaos.chaos(64_i64).should_not eq(64_i64)
end

# chaos return with Integer int128
it "chaos - int128: returns a correct value" do
chaos = Chaos::Chaos.new
chaos.probability = 0.0
chaos.chaos(128_i128).should eq(128_i128)
end

it "chaos - int128: returns a chaotic value" do
chaos = Chaos::Chaos.new
chaos.probability = 1.0
chaos.chaos(128_i128).should_not eq(128_i128)
end

# chaos return with Integer uint8
it "chaos - uint8: returns a correct value" do
chaos = Chaos::Chaos.new
chaos.probability = 0.0
chaos.chaos(8_u8).should eq(8_u8)
end

it "chaos - uint8: returns a chaotic value" do
chaos = Chaos::Chaos.new
chaos.probability = 1.0
chaos.chaos(8_u8).should_not eq(8_u8)
end

# chaos return with Integer uint16
it "chaos - uint16: returns a correct value" do
chaos = Chaos::Chaos.new
chaos.probability = 0.0
chaos.chaos(16_u16).should eq(16_u16)
end

it "chaos - uint16: returns a chaotic value" do
chaos = Chaos::Chaos.new
chaos.probability = 1.0
chaos.chaos(16_u16).should_not eq(16_u16)
end

# chaos return with Integer uint32
it "chaos - uint32: returns a correct value" do
chaos = Chaos::Chaos.new
chaos.probability = 0.0
chaos.chaos(32_u32).should eq(32_u32)
end

it "chaos - uint32: returns a chaotic value" do
chaos = Chaos::Chaos.new
chaos.probability = 1.0
chaos.chaos(32_u32).should_not eq(32_u32)
end

# chaos return with Integer uint64
it "chaos - uint64: returns a correct value" do
chaos = Chaos::Chaos.new
chaos.probability = 0.0
chaos.chaos(64_u64).should eq(64_u64)
end

it "chaos - uint64: returns a chaotic value" do
chaos = Chaos::Chaos.new
chaos.probability = 1.0
chaos.chaos(64_u64).should_not eq(64_u64)
end

# chaos return with Integer uint128
it "chaos - uint128: returns a correct value" do
chaos = Chaos::Chaos.new
chaos.probability = 0.0
chaos.chaos(128_u128).should eq(128_u128)
end

it "chaos - uint128: returns a chaotic value" do
chaos = Chaos::Chaos.new
chaos.probability = 1.0
chaos.chaos(128_u128).should_not eq(128_u128)
end

# chaos return with Float float32
it "chaos - float32: returns a correct value" do
chaos = Chaos::Chaos.new
chaos.probability = 0.0
chaos.chaos(32.0_f32).should eq(32.0_f32)
end

it "chaos - float32: returns a chaotic value" do
chaos = Chaos::Chaos.new
chaos.probability = 1.0
chaos.chaos(32.0_f32).should_not eq(32.0_f32)
end

# chaos return with Float float64
it "chaos - float64: returns a correct value" do
chaos = Chaos::Chaos.new
chaos.probability = 0.0
chaos.chaos(64.0_f64).should eq(64.0_f64)
end

it "chaos - float64: returns a chaotic value" do
chaos = Chaos::Chaos.new
chaos.probability = 1.0
chaos.chaos(64.0_f64).should_not eq(64.0_f64)
end

# chaos return with Char

# chaos return with String
Expand Down
36 changes: 36 additions & 0 deletions src/chaos.cr
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,64 @@ module Chaos
end

# Integer int8
def chaos(variable : Int8) : Int8
rand < @probability ? (variable = Random.rand(-128_i8..127_i8).to_i8) : variable
end

# Integer int16
def chaos(variable : Int16) : Int16
rand < @probability ? (variable = Random.rand(-32_768_i16..32_767_i16).to_i16) : variable
end

# Integer int32
def chaos(variable : Int32) : Int32
rand < @probability ? (variable = Random.rand(-2_147_483_648_i32..2_147_483_647_i32).to_i32) : variable
end

# Integer int64
def chaos(variable : Int64) : Int64
rand < @probability ? (variable = Random.rand(-9_223_372_036_854_775_808_i64..9_223_372_036_854_775_807_i64).to_i64) : variable
end

# Integer int128
def chaos(variable : Int128) : Int128
rand < @probability ? (variable = Random.rand(-170_141_183_460_469_231_731_687_303_715_884_105_728_i128..170_141_183_460_469_231_731_687_303_715_884_105_727_i128).to_i128) : variable
end

# Integer uint8
def chaos(variable : UInt8) : UInt8
rand < @probability ? (variable = Random.rand(0_u8..255_u8).to_u8) : variable
end

# Integer uint16
def chaos(variable : UInt16) : UInt16
rand < @probability ? (variable = Random.rand(0_u16..65_535_u16).to_u16) : variable
end

# Integer uint32
def chaos(variable : UInt32) : UInt32
rand < @probability ? (variable = Random.rand(0_u32..4_294_967_295_u32).to_u32) : variable
end

# Integer uint64
def chaos(variable : UInt64) : UInt64
rand < @probability ? (variable = Random.rand(0_u64..18_446_744_073_709_551_615_u64).to_u64) : variable
end

# Integer uint128
def chaos(variable : UInt128) : UInt128
rand < @probability ? (variable = Random.rand(0_u128..340_282_366_920_938_463_463_374_607_431_768_211_455_u128).to_u128) : variable
end

# Float float32
def chaos(variable : Float32) : Float32
rand < @probability ? (variable = Random.rand(0.0...1.0).to_f32) : variable
end

# Float float64
def chaos(variable : Float64) : Float64
rand < @probability ? (variable = Random.rand(0.0...1.0).to_f64) : variable
end

# Char

Expand Down

0 comments on commit a00859a

Please sign in to comment.