| 7 | ) |
| 8 | |
| 9 | func TestToCPUSet(t *testing.T) { |
| 10 | set := func(cpus ...int) *unix.CPUSet { |
| 11 | r := &unix.CPUSet{} |
| 12 | for _, cpu := range cpus { |
| 13 | r.Set(cpu) |
| 14 | } |
| 15 | return r |
| 16 | } |
| 17 | |
| 18 | testCases := []struct { |
| 19 | in string |
| 20 | out *unix.CPUSet |
| 21 | isErr bool |
| 22 | }{ |
| 23 | {in: ""}, // Empty means unset. |
| 24 | |
| 25 | // Valid cases. |
| 26 | {in: "0", out: &unix.CPUSet{1}}, |
| 27 | {in: "1", out: &unix.CPUSet{2}}, |
| 28 | {in: "0-1", out: &unix.CPUSet{3}}, |
| 29 | {in: "0,1", out: &unix.CPUSet{3}}, |
| 30 | {in: ",0,1,", out: &unix.CPUSet{3}}, |
| 31 | {in: "0-3", out: &unix.CPUSet{0x0f}}, |
| 32 | {in: "0,1,2-3", out: &unix.CPUSet{0x0f}}, |
| 33 | {in: "4-7", out: &unix.CPUSet{0xf0}}, |
| 34 | {in: "0-7", out: &unix.CPUSet{0xff}}, |
| 35 | {in: "0-15", out: &unix.CPUSet{0xffff}}, |
| 36 | {in: "16", out: &unix.CPUSet{0x10000}}, |
| 37 | // Extra whitespace in between ranges are OK. |
| 38 | {in: "1, 2, 1-2", out: &unix.CPUSet{6}}, |
| 39 | {in: " , 1 , 3 , 5-7, ", out: &unix.CPUSet{0xea}}, |
| 40 | // Somewhat large values. The underlying type in unix.CPUSet |
| 41 | // can either be uint32 or uint64, so we have to use a helper. |
| 42 | {in: "0-3,32-33", out: set(0, 1, 2, 3, 32, 33)}, |
| 43 | {in: "127-129, 1", out: set(1, 127, 128, 129)}, |
| 44 | {in: "1023", out: set(1023)}, |
| 45 | |
| 46 | // Error cases. |
| 47 | {in: "-", isErr: true}, |
| 48 | {in: "1-", isErr: true}, |
| 49 | {in: "-3", isErr: true}, |
| 50 | {in: ",", isErr: true}, |
| 51 | {in: " ", isErr: true}, |
| 52 | // Bad range (start > end). |
| 53 | {in: "54-53", isErr: true}, |
| 54 | // Extra spaces inside a range is not OK. |
| 55 | {in: "1 - 2", isErr: true}, |
| 56 | {in: "1024", isErr: true}, // Too big for unix.CPUSet. |
| 57 | } |
| 58 | |
| 59 | for _, tc := range testCases { |
| 60 | t.Run(tc.in, func(t *testing.T) { |
| 61 | out, err := ToCPUSet(tc.in) |
| 62 | t.Logf("ToCPUSet(%q) = %v (error: %v)", tc.in, out, err) |
| 63 | // Check the error. |
| 64 | if tc.isErr { |
| 65 | if err == nil { |
| 66 | t.Error("want error, got nil") |