advent/spec/util_spec.cr

94 lines
2.0 KiB
Crystal

require "./spec_helper"
describe Array do
describe "#union" do
it "works with no elements" do
([] of Set(Int32)).union.should eq(Set(Int32).new)
end
it "correctly performs a union" do
[Set { 1 }, Set { 2 }, Set {1, 2, 3}].union.should eq(Set { 1,2,3})
end
end
describe "#intersect" do
expect_raises(Enumerable::EmptyError) do
([] of Set(Int32)).intersect
end
it "correctly performs an intersection" do
[Set {1}, Set {1,2}, Set {1,2,3}].intersect.should eq(Set{1})
end
end
describe "#zip_with" do
it "performs as expected" do
[1,2,3].zip_with([2,3,4]) { |l,r| l*r }.should eq([2,6,12])
end
end
end
describe Tuple do
describe "#add" do
it "works for pairs" do
{1,2}.add({2,3}).should eq({3,5})
end
it "works for triples" do
{1,2,3}.add({3,4,5}).should eq({4, 6, 8})
end
end
describe "#neg" do
it "works for pairs" do
{1,2}.neg.should eq({-1,-2})
end
it "works for triples" do
{1,2,3}.neg.should eq({-1, -2, -3})
end
end
describe "#dot" do
it "works for pairs" do
{1,2}.dot({2,3}).should eq(8)
end
it "works for triples" do
{1,2,3}.dot({2,3,4}).should eq(20)
end
end
describe "#reduce_fraction" do
it "works with nonzero numbers" do
[{1, 3}, {2, 6}, {3, 9}].each(&.reduce_fraction.should eq({1,3}))
end
it "works when the denominator is zero" do
# This is technically not a fraction, but it helps to have
# consistent behavior
{5, 0}.reduce_fraction.should eq({1,0})
end
it "works when the numerator is zero" do
{0, 5}.reduce_fraction.should eq({0, 1})
end
end
end
describe Number do
describe "#signum" do
it "works properly for positive numbers" do
[1,2,3,4,5].each(&.signum.should eq(1))
end
it "works properly for negative numbers" do
[-1,-2,-3,-4].each(&.signum.should eq(-1))
end
it "works properly for zero" do
0.signum.should eq(0)
end
end
end