Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Atomics and Locks (ARM, AArch64, X86) #14293

Merged
66 changes: 51 additions & 15 deletions spec/std/atomic_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -97,57 +97,79 @@ describe Atomic do
atomic.compare_and_set([1], arr2).should eq({arr1, false})
atomic.get.should be(arr1)
end

it "explicit ordering" do
atomic = Atomic.new(1)

atomic.compare_and_set(2, 3, :acquire, :relaxed).should eq({1, false})
atomic.get.should eq(1)

atomic.compare_and_set(1, 3, :acquire_release, :relaxed).should eq({1, true})
atomic.get.should eq(3)
end
end

it "#adds" do
atomic = Atomic.new(1)
atomic.add(2).should eq(1)
atomic.get.should eq(3)
atomic.add(1, :relaxed).should eq(3)
atomic.get.should eq(4)
end

it "#sub" do
atomic = Atomic.new(1)
atomic.sub(2).should eq(1)
atomic.get.should eq(-1)
atomic.sub(1, :relaxed).should eq(-1)
atomic.get.should eq(-2)
end

it "#and" do
atomic = Atomic.new(5)
atomic.and(3).should eq(5)
atomic.get.should eq(1)
atomic.and(7, :relaxed).should eq(1)
atomic.get.should eq(1)
end

it "#nand" do
atomic = Atomic.new(5)
atomic.nand(3).should eq(5)
atomic.get.should eq(-2)
atomic.nand(7, :relaxed).should eq(-2)
atomic.get.should eq(-7)
end

it "#or" do
atomic = Atomic.new(5)
atomic.or(2).should eq(5)
atomic.get.should eq(7)
atomic.or(8, :relaxed).should eq(7)
atomic.get.should eq(15)
end

it "#xor" do
atomic = Atomic.new(5)
atomic.xor(3).should eq(5)
atomic.get.should eq(6)
atomic.xor(5, :relaxed).should eq(6)
atomic.get.should eq(3)
end

it "#max with signed" do
atomic = Atomic.new(5)
atomic.max(2).should eq(5)
atomic.get.should eq(5)
atomic.max(10).should eq(5)
atomic.max(10, :relaxed).should eq(5)
atomic.get.should eq(10)
end

it "#max with unsigned" do
atomic = Atomic.new(5_u32)
atomic.max(2_u32).should eq(5_u32)
atomic.get.should eq(5_u32)
atomic.max(UInt32::MAX).should eq(5_u32)
atomic.max(UInt32::MAX, :relaxed).should eq(5_u32)
atomic.get.should eq(UInt32::MAX)
end

Expand All @@ -165,15 +187,15 @@ describe Atomic do
atomic = Atomic.new(5)
atomic.min(10).should eq(5)
atomic.get.should eq(5)
atomic.min(2).should eq(5)
atomic.min(2, :relaxed).should eq(5)
atomic.get.should eq(2)
end

it "#min with unsigned" do
atomic = Atomic.new(UInt32::MAX)
atomic.min(10_u32).should eq(UInt32::MAX)
atomic.get.should eq(10_u32)
atomic.min(15_u32).should eq(10_u32)
atomic.min(15_u32, :relaxed).should eq(10_u32)
atomic.get.should eq(10_u32)
end

Expand All @@ -187,20 +209,28 @@ describe Atomic do
atomic.get.should eq(AtomicEnum::Minus)
end

it "#set" do
atomic = Atomic.new(1)
atomic.set(2).should eq(2)
atomic.get.should eq(2)
end
describe "#set" do
it "with integer" do
atomic = Atomic.new(1)
atomic.set(2).should eq(2)
atomic.get.should eq(2)
end

it "#set with nil (#4062)" do
atomic = Atomic(String?).new(nil)
it "with nil (#4062)" do
atomic = Atomic(String?).new(nil)

atomic.set("foo")
atomic.get.should eq("foo")
atomic.set("foo")
atomic.get.should eq("foo")

atomic.set(nil)
atomic.get.should eq(nil)
end

atomic.set(nil)
atomic.get.should eq(nil)
it "explicit ordering" do
atomic = Atomic.new(1)
atomic.set(0, :release).should eq(0)
atomic.get(:acquire).should eq(0)
end
end

it "#lazy_set" do
Expand Down Expand Up @@ -243,6 +273,12 @@ describe Atomic do
atomic.swap(arr1).should be(arr2)
atomic.get.should be(arr1)
end

it "explicit ordering" do
atomic = Atomic.new(1)
atomic.swap(2, :acquire).should eq(1)
atomic.get.should eq(2)
end
end
end

Expand Down