2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-06-09 15:43:54 +00:00
|
|
|
package pointer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2022-08-24 22:46:45 +00:00
|
|
|
"time"
|
2022-06-09 15:43:54 +00:00
|
|
|
|
2022-12-08 17:09:22 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2022-06-09 15:43:54 +00:00
|
|
|
"github.com/shoenig/test/must"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Of(t *testing.T) {
|
2022-12-08 17:09:22 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2022-06-09 15:43:54 +00:00
|
|
|
s := "hello"
|
|
|
|
sPtr := Of(s)
|
|
|
|
|
|
|
|
must.Eq(t, s, *sPtr)
|
|
|
|
|
|
|
|
b := "bye"
|
|
|
|
sPtr = &b
|
|
|
|
must.NotEq(t, s, *sPtr)
|
|
|
|
}
|
2022-08-24 22:46:45 +00:00
|
|
|
|
|
|
|
func Test_Copy(t *testing.T) {
|
2022-12-08 17:09:22 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2022-08-24 22:46:45 +00:00
|
|
|
orig := Of(1)
|
|
|
|
dup := Copy(orig)
|
|
|
|
orig = Of(7)
|
|
|
|
must.EqOp(t, 7, *orig)
|
|
|
|
must.EqOp(t, 1, *dup)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Compare(t *testing.T) {
|
2022-12-08 17:09:22 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2022-08-24 22:46:45 +00:00
|
|
|
t.Run("int", func(t *testing.T) {
|
|
|
|
a := 1
|
|
|
|
b := 2
|
|
|
|
c := 1
|
|
|
|
var n *int // nil
|
|
|
|
must.False(t, Eq(&a, &b))
|
|
|
|
must.True(t, Eq(&a, &c))
|
|
|
|
must.False(t, Eq(nil, &a))
|
|
|
|
must.False(t, Eq(n, &a))
|
|
|
|
must.True(t, Eq(n, nil))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("string", func(t *testing.T) {
|
|
|
|
a := "cat"
|
|
|
|
b := "dog"
|
|
|
|
c := "cat"
|
|
|
|
var n *string
|
|
|
|
|
|
|
|
must.False(t, Eq(&a, &b))
|
|
|
|
must.True(t, Eq(&a, &c))
|
|
|
|
must.False(t, Eq(nil, &a))
|
|
|
|
must.False(t, Eq(n, &a))
|
|
|
|
must.True(t, Eq(n, nil))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("duration", func(t *testing.T) {
|
|
|
|
a := time.Duration(1)
|
|
|
|
b := time.Duration(2)
|
|
|
|
c := time.Duration(1)
|
|
|
|
var n *time.Duration
|
|
|
|
|
|
|
|
must.False(t, Eq(&a, &b))
|
|
|
|
must.True(t, Eq(&a, &c))
|
|
|
|
must.False(t, Eq(nil, &a))
|
|
|
|
must.False(t, Eq(n, &a))
|
|
|
|
must.True(t, Eq(n, nil))
|
|
|
|
})
|
|
|
|
}
|
2022-12-08 17:09:22 +00:00
|
|
|
|
|
|
|
func Test_Merge(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
a := 1
|
|
|
|
b := 2
|
|
|
|
|
|
|
|
ptrA := &a
|
|
|
|
ptrB := &b
|
|
|
|
|
|
|
|
t.Run("exists", func(t *testing.T) {
|
|
|
|
result := Merge(ptrA, ptrB)
|
|
|
|
must.Eq(t, 2, *result)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
result := Merge(ptrA, nil)
|
|
|
|
must.Eq(t, 1, *result)
|
|
|
|
})
|
|
|
|
}
|