Debian Codegraph @

internal/version_test.go

raw symbols SCIP
1 package internal
2
3 import (
4 "testing"
5 )
6
7 func TestVersion(t *testing.T) {
8 a, err := NewVersion("1.2")
9 if err != nil {
10 t.Fatal(err)
11 }
12
13 b, err := NewVersion("2.2.1")
14 if err != nil {
15 t.Fatal(err)
16 }
17
18 if !a.Less(b) {
19 t.Error("A should be less than B")
20 }
21
22 if b.Less(a) {
23 t.Error("B shouldn't be less than A")
24 }
25
26 v200 := Version{2, 0, 0}
27 if !a.Less(v200) {
28 t.Error("1.2.1 should not be less than 2.0.0")
29 }
30
31 if v200.Less(a) {
32 t.Error("2.0.0 should not be less than 1.2.1")
33 }
34 }
35
36 func TestKernelVersion(t *testing.T) {
37 // Kernels 4.4 and 4.9 have a SUBLEVEL of over 255 and clamp it to 255.
38 // In our implementation, the other version segments are truncated.
39 if v, want := (Version{256, 256, 256}), uint32(255); v.Kernel() != want {
40 t.Errorf("256.256.256 should result in a kernel version of %d, got: %d", want, v.Kernel())
41 }
42
43 // Known good version.
44 if v, want := (Version{4, 9, 128}), uint32(264576); v.Kernel() != want {
45 t.Errorf("4.9.1 should result in a kernel version of %d, got: %d", want, v.Kernel())
46 }
47 }
48
49 func TestVersionFromCode(t *testing.T) {
50 var tests = []struct {
51 name string
52 code uint32
53 v Version
54 }{
55 {"0.0.0", 0, Version{0, 0, 0}},
56 {"1.0.0", 0x10000, Version{1, 0, 0}},
57 {"4.4.255", 0x404ff, Version{4, 4, 255}},
58 {"255.255.255", 0xffffff, Version{255, 255, 255}},
59 }
60
61 for _, tt := range tests {
62 t.Run(tt.name, func(t *testing.T) {
63 v := NewVersionFromCode(tt.code)
64 if v != tt.v {
65 t.Errorf("unexpected version for code '%d'. got: %v, want: %v", tt.code, v, tt.v)
66 }
67 })
68 }
69 }