summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-atomics/atomic_test.tpl
blob: 4e659d81fcd51459fcc28f964228cee23a857923 (plain)
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
package atomics_test

import (
    "atomic"
    "unsafe"
    "testing"

    "codeberg.org/gruf/go-atomics"
)

func Test{{ .Name }}StoreLoad(t *testing.T) {
    for _, test := range {{ .Name }}Tests {
        val := atomics.New{{ .Name }}()

        val.Store(test.V1)

        if !({{ call .Compare "val.Load()" "test.V1" }}) {
            t.Fatalf("failed testing .Store and .Load: expect=%v actual=%v", val.Load(), test.V1)
        }

        val.Store(test.V2)

        if !({{ call .Compare "val.Load()" "test.V2" }}) {
            t.Fatalf("failed testing .Store and .Load: expect=%v actual=%v", val.Load(), test.V2)
        }
    }
}

func Test{{ .Name }}CAS(t *testing.T) {
    for _, test := range {{ .Name }}Tests {
        val := atomics.New{{ .Name }}()

        val.Store(test.V1)

        if val.CAS(test.V2, test.V1) {
            t.Fatalf("failed testing negative .CAS: test=%+v state=%v", test, val.Load())
        }

        if !val.CAS(test.V1, test.V2) {
            t.Fatalf("failed testing positive .CAS: test=%+v state=%v", test, val.Load())
        }
    }
}

func Test{{ .Name }}Swap(t *testing.T) {
    for _, test := range {{ .Name }}Tests {
        val := atomics.New{{ .Name }}()

        val.Store(test.V1)

        if !({{ call .Compare "val.Swap(test.V2)" "test.V1" }}) {
            t.Fatal("failed testing .Swap")
        }

        if !({{ call .Compare "val.Swap(test.V1)" "test.V2" }}) {
            t.Fatal("failed testing .Swap")
        }
    }
}