|
| 1 | +package bytesranger |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestBytesranger_Split(t *testing.T) { |
| 9 | + cases := map[string]struct { |
| 10 | + number int |
| 11 | + numberOfPartitions int |
| 12 | + expected []string |
| 13 | + }{ |
| 14 | + "split 5 into 1": {number: 5, numberOfPartitions: 1, expected: []string{"bytes=0-4"}}, |
| 15 | + "split 5 into 2": {number: 5, numberOfPartitions: 2, expected: []string{"bytes=0-1", "bytes=2-4"}}, |
| 16 | + "split 5 into 3": {number: 5, numberOfPartitions: 3, expected: []string{"bytes=0-0", "bytes=1-1", "bytes=2-4"}}, |
| 17 | + "split 5 into 4": {number: 5, numberOfPartitions: 4, expected: []string{"bytes=0-0", "bytes=1-1", "bytes=2-2", "bytes=3-4"}}, |
| 18 | + "split 5 into 5": {number: 5, numberOfPartitions: 5, expected: []string{"bytes=0-0", "bytes=1-1", "bytes=2-2", "bytes=3-3", "bytes=4-4"}}, |
| 19 | + } |
| 20 | + |
| 21 | + for n, c := range cases { |
| 22 | + c := c |
| 23 | + t.Run(n, func(t *testing.T) { |
| 24 | + t.Parallel() |
| 25 | + |
| 26 | + number := c.number |
| 27 | + numberOfPartitions := c.numberOfPartitions |
| 28 | + expected := c.expected |
| 29 | + |
| 30 | + actual, err := Split(number, numberOfPartitions) |
| 31 | + if err != nil { |
| 32 | + t.Fatalf("err %s", err) |
| 33 | + } |
| 34 | + if !reflect.DeepEqual(actual, expected) { |
| 35 | + t.Errorf(`expected="%s" actual="%s"`, expected, actual) |
| 36 | + } |
| 37 | + }) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestBytesranger_Split_ZeroDivisionError(t *testing.T) { |
| 42 | + actual, err := Split(10, 0) |
| 43 | + if len(actual) != 0 { |
| 44 | + t.Fatalf("expected that length is 0, but %d", len(actual)) |
| 45 | + } |
| 46 | + if err != errZeroDivision { |
| 47 | + t.Errorf(`expected="%v" actual="%v"`, errZeroDivision, err) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestBytesranger_Split_NumberOfPartitionsError(t *testing.T) { |
| 52 | + actual, err := Split(10, 11) |
| 53 | + if len(actual) != 0 { |
| 54 | + t.Fatalf("expected that length is 0, but %d", len(actual)) |
| 55 | + } |
| 56 | + if err != errNumberOfPartitions { |
| 57 | + t.Errorf(`expected="%v" actual="%v"`, errNumberOfPartitions, err) |
| 58 | + } |
| 59 | +} |
0 commit comments