Adds unit tests

Former-commit-id: 33e9d9a8a370afc3d6318c6972de02a957fa3098
This commit is contained in:
2023-08-27 16:23:09 +02:00
parent 5b7d88ccc6
commit bfe6fa630a
3 changed files with 282 additions and 0 deletions

View File

@ -94,6 +94,8 @@ func (matrix *Matrix[T]) Rows(i ...int) Matrix[T] {
// It returns two integers: the number of rows and the number of columns.
func (matrix *Matrix[T]) Dim() (int, int) {
switch {
case matrix == nil:
return 0, 0
case *matrix == nil:
return 0, 0
case len(*matrix) == 0:

202
pkg/obiutils/array_test.go Normal file
View File

@ -0,0 +1,202 @@
package obiutils
import (
"reflect"
"testing"
)
func TestMake2DArray(t *testing.T) {
// Testing with rows = 0 and cols = 0
matrix := Make2DArray[int](0, 0)
if len(matrix) != 0 {
t.Errorf("Expected length of matrix to be 0, but got %d", len(matrix))
}
// Testing with rows = 3 and cols = 4
matrix = Make2DArray[int](3, 4)
if len(matrix) != 3 {
t.Errorf("Expected length of matrix to be 3, but got %d", len(matrix))
}
for i := 0; i < len(matrix); i++ {
if len(matrix[i]) != 4 {
t.Errorf("Expected length of row %d to be 4, but got %d", i, len(matrix[i]))
}
}
// Testing with rows = 2 and cols = 2 using string type
stringMatrix := Make2DArray[string](2, 2)
expectedStringMatrix := Matrix[string]{{"", ""}, {"", ""}}
if !reflect.DeepEqual(stringMatrix, expectedStringMatrix) {
t.Errorf("Expected matrix to be %v, but got %v", expectedStringMatrix, stringMatrix)
}
// Testing with rows = 4 and cols = 2 using custom struct type
type Person struct {
Name string
Age int
}
personMatrix := Make2DArray[Person](4, 2)
expectedPersonMatrix := Matrix[Person]{
{{Name: "", Age: 0}, {Name: "", Age: 0}},
{{Name: "", Age: 0}, {Name: "", Age: 0}},
{{Name: "", Age: 0}, {Name: "", Age: 0}},
{{Name: "", Age: 0}, {Name: "", Age: 0}},
}
if !reflect.DeepEqual(personMatrix, expectedPersonMatrix) {
t.Errorf("Expected matrix to be %v, but got %v", expectedPersonMatrix, personMatrix)
}
}
// TestMatrix_Column tests the Column method of the Matrix struct.
//
// Test case 1: Retrieving the first column of a 3x3 matrix of integers.
// Parameter(s): None.
// Return type(s): []int.
//
// Test case 2: Retrieving the second column of a 2x4 matrix of strings.
// Parameter(s): None.
// Return type(s): []string.
//
// Test case 3: Retrieving the third column of a 4x2 matrix of custom struct type.
// Parameter(s): None.
// Return type(s): []Person.
func TestMatrix_Column(t *testing.T) {
type Person struct {
Name string
Age int
}
// Test case 1: Retrieving the first column of a 3x3 matrix of integers
intMatrix := Matrix[int]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
expectedInt := []int{1, 4, 7}
resultInt := intMatrix.Column(0)
if !reflect.DeepEqual(resultInt, expectedInt) {
t.Errorf("Expected %v, but got %v", expectedInt, resultInt)
}
// Test case 2: Retrieving the second column of a 2x4 matrix of strings
stringMatrix := Matrix[string]{{"a", "b", "c", "d"}, {"e", "f", "g", "h"}}
expectedString := []string{"b", "f"}
resultString := stringMatrix.Column(1)
if !reflect.DeepEqual(resultString, expectedString) {
t.Errorf("Expected %v, but got %v", expectedString, resultString)
}
// Test case 3: Retrieving the third column of a 4x2 matrix of custom struct type
personMatrix := Matrix[Person]{
{{Name: "Alice", Age: 25}, {Name: "Bob", Age: 30}},
{{Name: "Charlie", Age: 35}, {Name: "Dave", Age: 40}},
{{Name: "Eve", Age: 45}, {Name: "Frank", Age: 50}},
{{Name: "Grace", Age: 55}, {Name: "Henry", Age: 60}},
}
expectedPerson := []Person{{Name: "Bob", Age: 30}, {Name: "Dave", Age: 40}, {Name: "Frank", Age: 50}, {Name: "Henry", Age: 60}}
resultPerson := personMatrix.Column(1)
if !reflect.DeepEqual(resultPerson, expectedPerson) {
t.Errorf("Expected %v, but got %v", expectedPerson, resultPerson)
}
}
// TestRows is a unit test function that tests the Rows method of the Matrix type in the Go program.
//
// It tests the behavior of the Rows method by providing different test cases, including single row,
// multiple rows, and empty rows, and comparing the actual output with the expected output.
//
// Parameters:
// - t: The testing.T type object for running the test cases and reporting any failures.
//
// Return: None.
func TestRows(t *testing.T) {
matrix := Matrix[int]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}
testCases := []struct {
name string
args []int
expected Matrix[int]
}{
{
name: "Single Row",
args: []int{1},
expected: Matrix[int]{{4, 5, 6}},
},
{
name: "Multiple Rows",
args: []int{0, 2},
expected: Matrix[int]{{1, 2, 3}, {7, 8, 9}},
},
{
name: "Empty Rows",
args: []int{},
expected: Matrix[int]{},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := matrix.Rows(tc.args...)
if !reflect.DeepEqual(got, tc.expected) {
t.Errorf("got %v, want %v", got, tc.expected)
}
})
}
}
// TestDim is a function that tests the `Dim` method of the `Matrix` struct.
//
// It tests the behavior of the `Dim` method using multiple test cases:
// 1. Test case: nil matrix
// 2. Test case: empty matrix
// 3. Test case: matrix with zero columns
// 4. Test case: non-empty matrix
// 5. Test case: generic matrix
//
// The function checks the dimensions returned by the `Dim` method and
// compares them with the expected dimensions for each test case. If the
// returned dimensions do not match the expected dimensions, it logs an
// error message using the `t.Errorf` function.
//
// Return type: void.
func TestDim(t *testing.T) {
// Test case: nil matrix
matrix := (*Matrix[int])(nil)
rows, cols := matrix.Dim()
if rows != 0 || cols != 0 {
t.Errorf("Expected dimensions (0, 0), got (%d, %d)", rows, cols)
}
// Test case: empty matrix
matrix = &Matrix[int]{}
rows, cols = matrix.Dim()
if rows != 0 || cols != 0 {
t.Errorf("Expected dimensions (0, 0), got (%d, %d)", rows, cols)
}
// Test case: matrix with zero columns
matrix = &Matrix[int]{{}}
rows, cols = matrix.Dim()
if rows != 1 || cols != 0 {
t.Errorf("Expected dimensions (1, 0), got (%d, %d)", rows, cols)
}
// Test case: non-empty matrix
matrix = &Matrix[int]{
{1, 2, 3},
{4, 5, 6},
}
rows, cols = matrix.Dim()
if rows != 2 || cols != 3 {
t.Errorf("Expected dimensions (2, 3), got (%d, %d)", rows, cols)
}
// Test case: generic matrix
genericMatrix := reflect.New(reflect.TypeOf(Matrix[int]{})).Interface()
rows, cols = genericMatrix.(*Matrix[int]).Dim()
if rows != 0 || cols != 0 {
t.Errorf("Expected dimensions (0, 0), got (%d, %d)", rows, cols)
}
}

View File

@ -0,0 +1,78 @@
package obiutils
import (
"reflect"
"testing"
)
// TestInPlaceToLower is a test function that tests the InPlaceToLower function.
//
// It tests the behavior of the InPlaceToLower function by providing different inputs and comparing the expected output.
// The function takes a slice of bytes as input and converts all uppercase letters to lowercase in-place.
// It returns nothing.
func TestInPlaceToLower(t *testing.T) {
tests := []struct {
name string
input []byte
expected []byte
}{
{
name: "All uppercase letters",
input: []byte("HELLO WORLD"),
expected: []byte("hello world"),
},
{
name: "Empty input",
input: []byte(""),
expected: []byte(""),
},
{
name: "No uppercase letters",
input: []byte("hello world"),
expected: []byte("hello world"),
},
{
name: "Mixed case letters",
input: []byte("Hello WoRlD"),
expected: []byte("hello world"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := InPlaceToLower(test.input)
if string(result) != string(test.expected) {
t.Errorf("Expected %s, but got %s", test.expected, result)
}
})
}
}
// TestMake2DNumericArray is a Go function that tests the Make2DNumericArray function.
//
// Parameter(s):
// - t: a pointer to the testing.T type
//
// Return type(s): None
func TestMake2DNumericArray(t *testing.T) {
// Test case 1: Create a 2D numeric array with 3 rows and 4 columns, not zeroed
matrix := Make2DNumericArray[int](3, 4, false)
expected := Matrix[int]{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
if !reflect.DeepEqual(matrix, expected) {
t.Errorf("Test case 1 failed. Expected %v, but got %v", expected, matrix)
}
// Test case 2: Create a 2D numeric array with 2 rows and 2 columns, zeroed
matrix = Make2DNumericArray[int](2, 2, true)
expected = Matrix[int]{{0, 0}, {0, 0}}
if !reflect.DeepEqual(matrix, expected) {
t.Errorf("Test case 2 failed. Expected %v, but got %v", expected, matrix)
}
// Test case 3: Create a 2D numeric array with 1 row and 1 column, zeroed
matrix = Make2DNumericArray[int](1, 1, true)
expected = Matrix[int]{{0}}
if !reflect.DeepEqual(matrix, expected) {
t.Errorf("Test case 3 failed. Expected %v, but got %v", expected, matrix)
}
}