github.com/opencontainers/runc@v1.2.0-rc.1.0.20240520010911-492dc558cdd6/libcontainer/cgroups/systemd/cpuset_test.go (about) 1 package systemd 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 func TestRangeToBits(t *testing.T) { 9 testCases := []struct { 10 in string 11 out []byte 12 isErr bool 13 }{ 14 {in: "", isErr: true}, 15 {in: "0", out: []byte{1}}, 16 {in: "1", out: []byte{2}}, 17 {in: "0-1", out: []byte{3}}, 18 {in: "0,1", out: []byte{3}}, 19 {in: ",0,1,", out: []byte{3}}, 20 {in: "0-3", out: []byte{0x0f}}, 21 {in: "0,1,2-3", out: []byte{0x0f}}, 22 {in: "4-7", out: []byte{0xf0}}, 23 {in: "0-7", out: []byte{0xff}}, 24 {in: "0-15", out: []byte{0xff, 0xff}}, 25 {in: "16", out: []byte{0, 0, 1}}, 26 {in: "0-3,32-33", out: []byte{0x0f, 0, 0, 0, 3}}, 27 // extra spaces and tabs are ok 28 {in: "1, 2, 1-2", out: []byte{6}}, 29 {in: " , 1 , 3 , 5-7, ", out: []byte{0xea}}, 30 // somewhat large values 31 {in: "128-130,1", out: []byte{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7}}, 32 33 {in: "-", isErr: true}, 34 {in: "1-", isErr: true}, 35 {in: "-3", isErr: true}, 36 // bad range (start > end) 37 {in: "54-53", isErr: true}, 38 // kernel does not allow extra spaces inside a range 39 {in: "1 - 2", isErr: true}, 40 } 41 42 for _, tc := range testCases { 43 out, err := RangeToBits(tc.in) 44 if err != nil { 45 if !tc.isErr { 46 t.Errorf("case %q: unexpected error: %v", tc.in, err) 47 } 48 49 continue 50 } 51 if !bytes.Equal(out, tc.out) { 52 t.Errorf("case %q: expected %v, got %v", tc.in, tc.out, out) 53 } 54 } 55 }