File: atomic_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (409 lines) | stat: -rw-r--r-- 11,147 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
require "spec"

enum AtomicEnum
  One
  Two
  Three
  Minus = -1
end

@[Flags]
enum AtomicEnumFlags
  One
  Two
  Three
end

private struct AtomicBooleans
  @one = Atomic(Bool).new(false)
  @two = Atomic(Bool).new(false)
  @three = Atomic(Bool).new(false)
end

describe Atomic do
  describe "#compare_and_set" do
    it "with bool" do
      atomic = Atomic.new(true)

      atomic.compare_and_set(false, true).should eq({true, false})
      atomic.get.should eq(true)

      atomic.compare_and_set(true, false).should eq({true, true})
      atomic.get.should eq(false)
    end

    it "with integer" do
      atomic = Atomic.new(1)

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

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

    it "with enum" do
      atomic = Atomic(AtomicEnum).new(AtomicEnum::One)

      atomic.compare_and_set(AtomicEnum::Two, AtomicEnum::Three).should eq({AtomicEnum::One, false})
      atomic.get.should eq(AtomicEnum::One)

      atomic.compare_and_set(AtomicEnum::One, AtomicEnum::Three).should eq({AtomicEnum::One, true})
      atomic.get.should eq(AtomicEnum::Three)
    end

    it "with flags enum" do
      atomic = Atomic(AtomicEnumFlags).new(AtomicEnumFlags::One)

      atomic.compare_and_set(AtomicEnumFlags::Two, AtomicEnumFlags::Three).should eq({AtomicEnumFlags::One, false})
      atomic.get.should eq(AtomicEnumFlags::One)

      atomic.compare_and_set(AtomicEnumFlags::One, AtomicEnumFlags::Three).should eq({AtomicEnumFlags::One, true})
      atomic.get.should eq(AtomicEnumFlags::Three)
    end

    it "with pointer" do
      atomic = Atomic.new(Pointer(Void).null)

      atomic.compare_and_set(Pointer(Void).new(1), Pointer(Void).new(3)).should eq({Pointer(Void).null, false})
      atomic.get.should eq(Pointer(Void).null)

      atomic.compare_and_set(Pointer(Void).null, Pointer(Void).new(3)).should eq({Pointer(Void).null, true})
      atomic.get.should eq(Pointer(Void).new(3))
    end

    it "with nilable reference" do
      atomic = Atomic(String?).new(nil)
      string = "hello"

      atomic.compare_and_set(string, "foo").should eq({nil, false})
      atomic.get.should be_nil

      atomic.compare_and_set(nil, string).should eq({nil, true})
      atomic.get.should be(string)

      atomic.compare_and_set(string, nil).should eq({string, true})
      atomic.get.should be_nil
    end

    it "with reference type" do
      str1 = "hello"
      str2 = "bye"

      atomic = Atomic(String).new(str1)

      atomic.compare_and_set(str2, "foo").should eq({str1, false})
      atomic.get.should be(str1)

      atomic.compare_and_set(str1, str2).should eq({str1, true})
      atomic.get.should be(str2)

      atomic.compare_and_set(str2, str1).should eq({str2, true})
      atomic.get.should be(str1)

      atomic.compare_and_set(String.build(&.<< "bye"), str2).should eq({str1, false})
      atomic.get.should be(str1)
    end

    it "with reference union" do
      arr1 = [1]
      arr2 = [""]

      atomic = Atomic(Array(Int32) | Array(String)).new(arr1)

      atomic.compare_and_set(arr2, ["foo"]).should eq({arr1, false})
      atomic.get.should be(arr1)

      atomic.compare_and_set(arr1, arr2).should eq({arr1, true})
      atomic.get.should be(arr2)

      atomic.compare_and_set(arr2, arr1).should eq({arr2, true})
      atomic.get.should be(arr1)

      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, :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, :relaxed).should eq(5_u32)
    atomic.get.should eq(UInt32::MAX)
  end

  it "#max with signed enum" do
    atomic = Atomic.new(AtomicEnum::Two)
    atomic.max(AtomicEnum::One).should eq(AtomicEnum::Two)
    atomic.get.should eq(AtomicEnum::Two)
    atomic.max(AtomicEnum::Three).should eq(AtomicEnum::Two)
    atomic.get.should eq(AtomicEnum::Three)
    atomic.max(AtomicEnum::Minus).should eq(AtomicEnum::Three)
    atomic.get.should eq(AtomicEnum::Three)
  end

  it "#max with pointer type" do
    atomic = Atomic.new(Pointer(Void).new(2))
    atomic.max(Pointer(Void).new(1)).should eq(Pointer(Void).new(2))
    atomic.get.should eq(Pointer(Void).new(2))
    atomic.max(Pointer(Void).new(3)).should eq(Pointer(Void).new(2))
    atomic.get.should eq(Pointer(Void).new(3))
    atomic.max(Pointer(Void).new(UInt64::MAX)).should eq(Pointer(Void).new(3))
    atomic.get.should eq(Pointer(Void).new(UInt64::MAX))
  end

  it "#min with signed" do
    atomic = Atomic.new(5)
    atomic.min(10).should eq(5)
    atomic.get.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, :relaxed).should eq(10_u32)
    atomic.get.should eq(10_u32)
  end

  it "#min with signed enum" do
    atomic = Atomic.new(AtomicEnum::Two)
    atomic.min(AtomicEnum::Three).should eq(AtomicEnum::Two)
    atomic.get.should eq(AtomicEnum::Two)
    atomic.min(AtomicEnum::One).should eq(AtomicEnum::Two)
    atomic.get.should eq(AtomicEnum::One)
    atomic.min(AtomicEnum::Minus).should eq(AtomicEnum::One)
    atomic.get.should eq(AtomicEnum::Minus)
  end

  it "#min with pointer type" do
    atomic = Atomic.new(Pointer(Void).new(2))
    atomic.min(Pointer(Void).new(3)).should eq(Pointer(Void).new(2))
    atomic.get.should eq(Pointer(Void).new(2))
    atomic.min(Pointer(Void).new(1)).should eq(Pointer(Void).new(2))
    atomic.get.should eq(Pointer(Void).new(1))
    atomic.min(Pointer(Void).new(UInt64::MAX)).should eq(Pointer(Void).new(1))
    atomic.get.should eq(Pointer(Void).new(1))
  end

  describe "#set" do
    it "with bool" do
      atomic = Atomic.new(false)
      atomic.set(true).should eq(true)
      atomic.get.should eq(true)
    end

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

    it "with pointer type" do
      atomic = Atomic.new(Pointer(Void).new(1))
      atomic.set(Pointer(Void).new(3)).should eq(Pointer(Void).new(3))
      atomic.get.should eq(Pointer(Void).new(3))
    end

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

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

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

    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
    atomic = Atomic.new(1)
    atomic.lazy_set(2).should eq(2)
    atomic.lazy_get.should eq(2)

    bool = Atomic.new(true)
    bool.lazy_set(false).should eq(false)
    bool.lazy_get.should eq(false)
  end

  describe "#swap" do
    it "with bool" do
      atomic = Atomic.new(true)
      atomic.swap(false).should eq(true)
      atomic.get.should eq(false)
    end

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

    it "with pointer type" do
      atomic = Atomic.new(Pointer(Void).new(1))
      atomic.swap(Pointer(Void).new(3)).should eq(Pointer(Void).new(1))
      atomic.get.should eq(Pointer(Void).new(3))
    end

    it "with reference type" do
      atomic = Atomic.new("hello")
      atomic.swap("world").should eq("hello")
      atomic.get.should eq("world")
    end

    it "with nilable reference" do
      atomic = Atomic(String?).new(nil)

      atomic.swap("not nil").should eq(nil)
      atomic.get.should eq("not nil")

      atomic.swap(nil).should eq("not nil")
      atomic.get.should eq(nil)
    end

    it "with reference union" do
      arr1 = [1]
      arr2 = [""]
      atomic = Atomic(Array(Int32) | Array(String)).new(arr1)

      atomic.swap(arr2).should be(arr1)
      atomic.get.should be(arr2)

      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

  describe "atomic bool" do
    it "sizeof" do
      sizeof(Atomic(Bool)).should eq(1)
      sizeof(AtomicBooleans).should eq(3)
    end

    it "gets and sets" do
      booleans = AtomicBooleans.new

      booleans.@one.get.should eq(false)
      booleans.@two.get.should eq(false)
      booleans.@three.get.should eq(false)

      booleans.@two.set(true)
      booleans.@one.get.should eq(false)
      booleans.@two.get.should eq(true)
      booleans.@three.get.should eq(false)

      booleans.@one.set(true)
      booleans.@three.set(true)
      booleans.@one.get.should eq(true)
      booleans.@two.get.should eq(true)
      booleans.@three.get.should eq(true)

      booleans.@one.set(false)
      booleans.@three.set(false)
      booleans.@one.get.should eq(false)
      booleans.@two.get.should eq(true)
      booleans.@three.get.should eq(false)

      booleans.@two.set(false)
      booleans.@one.get.should eq(false)
      booleans.@two.get.should eq(false)
      booleans.@three.get.should eq(false)
    end
  end
end

describe Atomic::Flag do
  it "#test_and_set" do
    flag = Atomic::Flag.new
    flag.test_and_set.should be_true
    flag.test_and_set.should be_false
  end

  it "#clear" do
    flag = Atomic::Flag.new
    flag.test_and_set.should be_true
    flag.clear
    flag.test_and_set.should be_true
  end
end