2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-10-12 06:18:26 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
2018-10-12 22:25:34 +00:00
|
|
|
"fmt"
|
2018-10-12 06:18:26 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-08-17 16:26:34 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pointer"
|
2018-10-12 06:18:26 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-10-12 22:25:34 +00:00
|
|
|
func TestAttribute_Validate(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input *Attribute
|
|
|
|
Fail bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Input: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Bool: pointer.Of(true),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
String: pointer.Of("foo"),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(123)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(123.2)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Bool: pointer.Of(true),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MB",
|
|
|
|
},
|
|
|
|
Fail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
String: pointer.Of("foo"),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MB",
|
|
|
|
},
|
|
|
|
Fail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(123)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "lolNO",
|
|
|
|
},
|
|
|
|
Fail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(123.2)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "lolNO",
|
|
|
|
},
|
|
|
|
Fail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(123)),
|
|
|
|
Float: pointer.Of(float64(123.2)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "mW",
|
|
|
|
},
|
|
|
|
Fail: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.Input.GoString(), func(t *testing.T) {
|
|
|
|
if err := c.Input.Validate(); err != nil && !c.Fail {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type compareTestCase struct {
|
|
|
|
A *Attribute
|
|
|
|
B *Attribute
|
|
|
|
Expected int
|
|
|
|
NotComparable bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttribute_Compare_Bool(t *testing.T) {
|
|
|
|
cases := []*compareTestCase{
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Bool: pointer.Of(true),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Bool: pointer.Of(true),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
Expected: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Bool: pointer.Of(true),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Bool: pointer.Of(false),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
Expected: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Bool: pointer.Of(true),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
String: pointer.Of("foo"),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
NotComparable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Bool: pointer.Of(true),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(123)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
NotComparable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Bool: pointer.Of(true),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(123.2)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
NotComparable: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testComparison(t, cases)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttribute_Compare_String(t *testing.T) {
|
|
|
|
cases := []*compareTestCase{
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
String: pointer.Of("a"),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
String: pointer.Of("b"),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
Expected: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
String: pointer.Of("hello"),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
String: pointer.Of("hello"),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
Expected: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
String: pointer.Of("b"),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
String: pointer.Of("a"),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
Expected: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
String: pointer.Of("hello"),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Bool: pointer.Of(true),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
NotComparable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
String: pointer.Of("hello"),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(123)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
NotComparable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
String: pointer.Of("hello"),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(123.2)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
NotComparable: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testComparison(t, cases)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttribute_Compare_Float(t *testing.T) {
|
|
|
|
cases := []*compareTestCase{
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(101.5)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(100001.5)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
Expected: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(100001.5)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(100001.5)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
Expected: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(999999999.5)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(101.5)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
Expected: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(101.5)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Bool: pointer.Of(true),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
NotComparable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(101.5)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
String: pointer.Of("hello"),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
NotComparable: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testComparison(t, cases)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttribute_Compare_Int(t *testing.T) {
|
|
|
|
cases := []*compareTestCase{
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(3)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(10)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
Expected: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(10)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(10)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
Expected: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(100)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(10)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
Expected: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(10)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Bool: pointer.Of(true),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
NotComparable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(10)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
String: pointer.Of("hello"),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
NotComparable: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testComparison(t, cases)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttribute_Compare_Int_With_Units(t *testing.T) {
|
|
|
|
cases := []*compareTestCase{
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(3)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MB",
|
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(10)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MB",
|
|
|
|
},
|
|
|
|
Expected: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(10)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MB",
|
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(10)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MB",
|
|
|
|
},
|
|
|
|
Expected: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(100)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MB",
|
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(10)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MB",
|
|
|
|
},
|
|
|
|
Expected: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(3)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "GB",
|
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(3)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MB",
|
|
|
|
},
|
|
|
|
Expected: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(1)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "GiB",
|
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(1024)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MiB",
|
|
|
|
},
|
|
|
|
Expected: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(1)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "GiB",
|
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(1025)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MiB",
|
|
|
|
},
|
|
|
|
Expected: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(1000)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "mW",
|
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(1)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "W",
|
|
|
|
},
|
|
|
|
Expected: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testComparison(t, cases)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttribute_Compare_Float_With_Units(t *testing.T) {
|
|
|
|
cases := []*compareTestCase{
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(3.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MB",
|
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(10.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MB",
|
|
|
|
},
|
|
|
|
Expected: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(10.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MB",
|
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(10.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MB",
|
|
|
|
},
|
|
|
|
Expected: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(100.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MB",
|
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(10.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MB",
|
|
|
|
},
|
|
|
|
Expected: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(3.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "GB",
|
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(3.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MB",
|
|
|
|
},
|
|
|
|
Expected: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(1.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "GiB",
|
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(1024.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MiB",
|
|
|
|
},
|
|
|
|
Expected: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(1.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "GiB",
|
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(1025.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MiB",
|
|
|
|
},
|
|
|
|
Expected: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(1000.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "mW",
|
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(1.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "W",
|
|
|
|
},
|
|
|
|
Expected: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(1.5)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "GiB",
|
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(1400.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
Unit: "MiB",
|
|
|
|
},
|
|
|
|
Expected: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testComparison(t, cases)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttribute_Compare_IntToFloat(t *testing.T) {
|
|
|
|
cases := []*compareTestCase{
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(3)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(10.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
Expected: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(10)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(10.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
Expected: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(10)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(10.1)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
Expected: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(100)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(10.0)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
Expected: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
A: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(100)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
B: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(100.00001)),
|
2018-10-12 22:25:34 +00:00
|
|
|
},
|
|
|
|
Expected: -1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testComparison(t, cases)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testComparison(t *testing.T, cases []*compareTestCase) {
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(fmt.Sprintf("%#v vs %#v", c.A, c.B), func(t *testing.T) {
|
|
|
|
v, ok := c.A.Compare(c.B)
|
|
|
|
if !ok && !c.NotComparable {
|
|
|
|
t.Fatal("should be comparable")
|
|
|
|
} else if ok {
|
|
|
|
require.Equal(t, c.Expected, v)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 06:18:26 +00:00
|
|
|
func TestAttribute_ParseAndValidate(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Expected *Attribute
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Input: "true",
|
|
|
|
Expected: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Bool: pointer.Of(true),
|
2018-10-12 06:18:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "false",
|
|
|
|
Expected: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Bool: pointer.Of(false),
|
2018-10-12 06:18:26 +00:00
|
|
|
},
|
|
|
|
},
|
2018-10-14 01:38:08 +00:00
|
|
|
{
|
|
|
|
Input: "1",
|
|
|
|
Expected: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(1)),
|
2018-10-14 01:38:08 +00:00
|
|
|
},
|
|
|
|
},
|
2018-10-12 06:18:26 +00:00
|
|
|
{
|
|
|
|
Input: "100",
|
|
|
|
Expected: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(100)),
|
2018-10-12 06:18:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "-100",
|
|
|
|
Expected: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(-100)),
|
2018-10-12 06:18:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "-1.0",
|
|
|
|
Expected: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(-1.0)),
|
2018-10-12 06:18:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "-100.25",
|
|
|
|
Expected: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(-100.25)),
|
2018-10-12 06:18:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "1.01",
|
|
|
|
Expected: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(1.01)),
|
2018-10-12 06:18:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "100.25",
|
|
|
|
Expected: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(100.25)),
|
2018-10-12 06:18:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "foobar",
|
|
|
|
Expected: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
String: pointer.Of("foobar"),
|
2018-10-12 06:18:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "foo123bar",
|
|
|
|
Expected: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
String: pointer.Of("foo123bar"),
|
2018-10-12 06:18:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "100MB",
|
|
|
|
Expected: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(100)),
|
2018-10-12 06:18:26 +00:00
|
|
|
Unit: "MB",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "-100MHz",
|
|
|
|
Expected: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Int: pointer.Of(int64(-100)),
|
2018-10-12 06:18:26 +00:00
|
|
|
Unit: "MHz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "-1.0MB/s",
|
|
|
|
Expected: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(-1.0)),
|
2018-10-12 06:18:26 +00:00
|
|
|
Unit: "MB/s",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "-100.25GiB/s",
|
|
|
|
Expected: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(-100.25)),
|
2018-10-12 06:18:26 +00:00
|
|
|
Unit: "GiB/s",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "1.01TB",
|
|
|
|
Expected: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(1.01)),
|
2018-10-12 06:18:26 +00:00
|
|
|
Unit: "TB",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "100.25mW",
|
|
|
|
Expected: &Attribute{
|
2022-08-17 16:26:34 +00:00
|
|
|
Float: pointer.Of(float64(100.25)),
|
2018-10-12 06:18:26 +00:00
|
|
|
Unit: "mW",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.Input, func(t *testing.T) {
|
|
|
|
a := ParseAttribute(c.Input)
|
|
|
|
require.Equal(t, c.Expected, a)
|
|
|
|
require.NoError(t, a.Validate())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-10-12 22:25:34 +00:00
|
|
|
|
|
|
|
func BenchmarkParse(b *testing.B) {
|
|
|
|
cases := []string{
|
|
|
|
"true",
|
|
|
|
"false",
|
|
|
|
"100",
|
|
|
|
"-100",
|
|
|
|
"-1.0",
|
|
|
|
"-100.25",
|
|
|
|
"1.01",
|
|
|
|
"100.25",
|
|
|
|
"foobar",
|
|
|
|
"foo123bar",
|
|
|
|
"100MB",
|
|
|
|
"-100MHz",
|
|
|
|
"-1.0MB/s",
|
|
|
|
"-100.25GiB/s",
|
|
|
|
"1.01TB",
|
|
|
|
"100.25mW",
|
|
|
|
}
|
|
|
|
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
for _, c := range cases {
|
|
|
|
ParseAttribute(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|