Skip to content

Commit b79fe9f

Browse files
authored
add nodesets tests (#41)
1 parent d0aa7cc commit b79fe9f

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

pkg/nodesets/nodesets_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright 2015-2025 CEA/DAM/DIF
2+
// Author: Arnaud Guignard <[email protected]>
3+
// Contributor: Cyril Servant <[email protected]>
4+
//
5+
// This software is governed by the CeCILL-B license under French law and
6+
// abiding by the rules of distribution of free software. You can use,
7+
// modify and/ or redistribute the software under the terms of the CeCILL-B
8+
// license as circulated by CEA, CNRS and INRIA at the following URL
9+
// "http://www.cecill.info".
10+
11+
package nodesets
12+
13+
import (
14+
"reflect"
15+
"testing"
16+
"unsafe"
17+
)
18+
19+
var goStringTests = []struct {
20+
input1, input2, want string
21+
}{
22+
// empty string
23+
{"", "", ""},
24+
// string ending with \0
25+
{"cyril", "", "cyril"},
26+
// string with a \0 in the middle
27+
{"test", "cyril", "test"},
28+
}
29+
30+
func TestGoString(t *testing.T) {
31+
// nil pointer
32+
got := goString(uintptr(unsafe.Pointer(nil)))
33+
if got != "" {
34+
t.Errorf("goString got \"%s\", want \"\"", got)
35+
}
36+
// strings
37+
for _, tt := range goStringTests {
38+
b := append([]byte(tt.input1), 0)
39+
b = append(b, []byte(tt.input2)...)
40+
got := goString(uintptr(unsafe.Pointer(&b[0])))
41+
if got != tt.want {
42+
t.Errorf("goString got \"%s\", want \"%s\"", got, tt.want)
43+
}
44+
}
45+
}
46+
47+
func BenchmarkGoString(b *testing.B) {
48+
// nil pointer
49+
b.Run("nil", func(b *testing.B) {
50+
for i := 0; i < b.N; i++ {
51+
goString(uintptr(unsafe.Pointer(nil)))
52+
}
53+
})
54+
// strings
55+
for _, tt := range goStringTests {
56+
b.Run(tt.input1+"_"+tt.input2, func(b *testing.B) {
57+
for i := 0; i < b.N; i++ {
58+
b := append([]byte(tt.input1), 0)
59+
b = append(b, []byte(tt.input2)...)
60+
goString(uintptr(unsafe.Pointer(&b[0])))
61+
}
62+
})
63+
}
64+
}
65+
66+
var InitExpanderTests = []struct {
67+
ns, err string
68+
want []string
69+
}{
70+
{"@test", "must not contain the @ character", []string{}},
71+
{"test", "", []string{"test"}},
72+
{"server[1,3-5]", "", []string{"server1", "server3", "server4", "server5"}},
73+
{"server[1-2],server4", "", []string{"server1", "server2", "server4"}},
74+
{"server[1-2", "unbalanced '[' found while parsing server[1-2 - nodeset parse error", []string{}},
75+
}
76+
77+
func TestInitExpander(t *testing.T) {
78+
got, close_func, expand_func := InitExpander()
79+
want := "libnodeset.so not found, falling back to iskylite's implementation"
80+
if got != want {
81+
t.Errorf("InitExpander got \"%s\", want \"%s\"", got, want)
82+
} else {
83+
for _, tt := range InitExpanderTests {
84+
got, err := expand_func(tt.ns)
85+
if err == nil && tt.err != "" {
86+
t.Errorf("got no error, want %s", tt.err)
87+
} else if err != nil && err.Error() != tt.err {
88+
t.Errorf("ERROR: %s, want %s", err, tt.err)
89+
} else if err == nil {
90+
if !reflect.DeepEqual(got, tt.want) {
91+
t.Errorf("got:\n%v\nwant:\n%v", got, tt.want)
92+
}
93+
}
94+
}
95+
err := close_func()
96+
if err != nil {
97+
t.Errorf("InitExpander close function got \"%s\", want nil", err)
98+
}
99+
}
100+
}
101+
102+
func BenchmarkInitExpander(b *testing.B) {
103+
for _, tt := range InitExpanderTests {
104+
b.Run(tt.ns, func(b *testing.B) {
105+
_, close_func, expand_func := InitExpander()
106+
expand_func(tt.ns)
107+
close_func()
108+
})
109+
}
110+
}

0 commit comments

Comments
 (0)