|
package arrays_test |
|
|
|
import ( |
|
"context" |
|
arr "github.com/derekkenney/theorem/arrays" |
|
"reflect" |
|
"testing" |
|
) |
|
|
|
const ( |
|
checkMark = "\u2713" |
|
ballotX = "\u2717" |
|
) |
|
|
|
var ( |
|
input = [][]int{{1}, {3, 2}, {4, 5}, {6, 7}} |
|
output = []int{1, 2, 3, 4, 5, 6, 7} |
|
) |
|
|
|
//TestFlattenIntArray tests that an input of nested int arrays, can be returned as a flat arry of ints |
|
func TestFlattenIntArrays(t *testing.T) { |
|
a := arr.IntArray{} |
|
t.Log("Given the need to flatten an array of nested int arrays") |
|
{ |
|
t.Logf("When calling FlattenIntArrays(%v)\n", input) |
|
{ |
|
//on error return empty arr, log error |
|
results, err := a.FlattenIntArrays(context.Background(), input) |
|
if err != nil { |
|
t.Errorf(err.Error()) |
|
} |
|
|
|
if results != nil { |
|
t.Logf("\tWe expect a single array of ints back %v", checkMark) |
|
} else { |
|
t.Errorf("\tWe expect a single array of ints back %v", ballotX) |
|
} |
|
|
|
if reflect.DeepEqual(results, output) { |
|
t.Logf("\t\tand the results to be %v, %v", results, checkMark) |
|
} |
|
if !reflect.DeepEqual(results, output) { |
|
t.Errorf("\t\tand the results to be %v, not %v. %v", output, results, ballotX) |
|
} |
|
|
|
} |
|
} |
|
} |
|
func TestHandleNullArgumentPredictably(t *testing.T) { |
|
a := arr.IntArray{} |
|
input = nil |
|
output = []int{} |
|
|
|
t.Log("When calling FlattenInArrays()\n") |
|
{ |
|
t.Logf("\tWith an array input that's nil") |
|
{ |
|
//on error return empty arr, log error |
|
results, err := a.FlattenIntArrays(context.Background(), input) |
|
|
|
if err != nil && err.Error() == "Input argument arr is nil" { |
|
t.Logf("\t\tWe expect an error type, %v", checkMark) |
|
} else { |
|
t.Errorf("\t\ttWe expect an error type, %v", ballotX) |
|
} |
|
|
|
if reflect.DeepEqual(results, output) { |
|
t.Logf("\t\t\tand an empty []int %v", checkMark) |
|
} else { |
|
t.Errorf("\t\t\tand an empty []int %v", ballotX) |
|
} |
|
|
|
} |
|
} |
|
} |