Skip to content

Commit fb09932

Browse files
committed
test(hypervisors): add unit tests for bytesToMiB and bytesToMB
Adds table-driven tests for the two byte-conversion helpers in pkg/unikontainers/hypervisors/utils.go, using testify/assert. Each helper is covered for zero, sub-unit, exact-unit, multiple, and large-value inputs. PR: #584 Signed-off-by: Parth Dagia <parth.24bcs10414@sst.scaler.com> Reviewed-by: Charalampos Mainas <cmainas@nubificus.co.uk> Approved-by: Charalampos Mainas <cmainas@nubificus.co.uk>
1 parent c7f8286 commit fb09932

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) 2023-2026, Nubificus LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package hypervisors
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
)
22+
23+
func TestBytesToMiB(t *testing.T) {
24+
t.Parallel()
25+
26+
const mib uint64 = 1024 * 1024
27+
28+
cases := []struct {
29+
name string
30+
input uint64
31+
expected uint64
32+
}{
33+
{"zero", 0, 0},
34+
{"less than one MiB truncates to zero", mib - 1, 0},
35+
{"exactly one MiB", mib, 1},
36+
{"exactly two MiB", 2 * mib, 2},
37+
{"non-multiple truncates down", mib + (mib / 2), 1},
38+
{"large value", 1024 * mib, 1024},
39+
}
40+
41+
for _, tc := range cases {
42+
t.Run(tc.name, func(t *testing.T) {
43+
t.Parallel()
44+
assert.Equal(t, tc.expected, bytesToMiB(tc.input))
45+
})
46+
}
47+
}
48+
49+
func TestBytesToMB(t *testing.T) {
50+
t.Parallel()
51+
52+
const mb uint64 = 1000 * 1000
53+
54+
cases := []struct {
55+
name string
56+
input uint64
57+
expected uint64
58+
}{
59+
{"zero", 0, 0},
60+
{"less than one MB truncates to zero", mb - 1, 0},
61+
{"exactly one MB", mb, 1},
62+
{"exactly two MB", 2 * mb, 2},
63+
{"non-multiple truncates down", mb + (mb / 2), 1},
64+
{"large value", 1024 * mb, 1024},
65+
}
66+
67+
for _, tc := range cases {
68+
t.Run(tc.name, func(t *testing.T) {
69+
t.Parallel()
70+
assert.Equal(t, tc.expected, bytesToMB(tc.input))
71+
})
72+
}
73+
}

0 commit comments

Comments
 (0)