|
| 1 | +package configs |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "golang.org/x/sys/unix" |
| 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 | + tc := tc |
| 61 | + t.Run(tc.in, func(t *testing.T) { |
| 62 | + out, err := toCPUSet(tc.in) |
| 63 | + t.Logf("toCPUSet(%q) = %v (error: %v)", tc.in, out, err) |
| 64 | + // Check the error. |
| 65 | + if tc.isErr { |
| 66 | + if err == nil { |
| 67 | + t.Error("want error, got nil") |
| 68 | + } |
| 69 | + return // No more checks. |
| 70 | + } |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("want no error, got %v", err) |
| 73 | + } |
| 74 | + // Check the value. |
| 75 | + if tc.out == nil { |
| 76 | + if out != nil { |
| 77 | + t.Fatalf("want nil, got %v", out) |
| 78 | + } |
| 79 | + return // No more checks. |
| 80 | + } |
| 81 | + if out == nil { |
| 82 | + t.Fatalf("want %v, got nil", tc.out) |
| 83 | + } |
| 84 | + if *out != *tc.out { |
| 85 | + t.Errorf("case %q: want %v, got %v", tc.in, tc.out, out) |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
0 commit comments