github.com/synesissoftware/angols@v0.0.0-20190330004400-955d82dbf73b/collect_test.go (about)

     1  
     2  package angols_test
     3  
     4  import (
     5  
     6  	"github.com/synesissoftware/ANGoLS"
     7  
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  const (
    13  
    14  	int_equal_iterations	=	100000
    15  	int_equal_size			=	100
    16  )
    17  
    18  
    19  // Tests
    20  
    21  func Test_Collect_Array_1_ints_to_ints(t *testing.T) {
    22  
    23  	fn := func(input interface{}) (interface{}, error) {
    24  
    25  		if i, ok := input.(int); ok {
    26  
    27  			if i < 0 {
    28  
    29  				return -i, nil
    30  			}
    31  		}
    32  
    33  		return input, nil
    34  	}
    35  
    36  	ints	:=	[]int { 1, 2, 3 }
    37  
    38  	r, err	:=	angols.CollectSlice(ints, fn)
    39  	if err != nil {
    40  
    41  		t.Errorf("Collect() failed: %v", err)
    42  	} else {
    43  
    44  		if si, ok := r.([]interface{}); !ok {
    45  
    46  			t.Errorf("Collect() failed: result is not of type []interface{}\n")
    47  		} else {
    48  
    49  			for ix, v := range si {
    50  
    51  				if i, ok := v.(int); !ok {
    52  
    53  					t.Errorf("Element (%T)%v of non-int type at index %d\n", v, v, ix)
    54  				} else {
    55  
    56  					if ints[ix] != i {
    57  
    58  						t.Errorf("Element (%T)%v at index %d is different from expected value %d\n", v, v, ix, ints[ix])
    59  					}
    60  				}
    61  			}
    62  		}
    63  	}
    64  }
    65  
    66  func Test_CollectSliceOfInt_1(t *testing.T) {
    67  
    68  	fn := func(input int) int {
    69  
    70  		if input < 0 {
    71  
    72  			return -input
    73  		}
    74  
    75  		return input
    76  	}
    77  
    78  	input_slice	:=	[]int { -1, 0, -2, 3 }
    79  	expected	:=	[]int {  1, 0,  2, 3 }
    80  	actual		:=	angols.CollectSliceOfInt(input_slice, fn)
    81  
    82  	if !angols.EqualSliceOfInt(expected, actual) {
    83  
    84  		t.Errorf("actual value '%v' does not equal expected value '%v'", actual, expected)
    85  	}
    86  
    87  	if !angols.EqualSlice(expected, actual) {
    88  
    89  		t.Errorf("actual value '%v' does not equal expected value '%v'", actual, expected)
    90  	}
    91  }
    92  
    93  func Test_CollectSliceOfFloat64_1(t *testing.T) {
    94  
    95  	fn := func(input float64) float64 {
    96  
    97  		if input < 0 {
    98  
    99  			return -input
   100  		}
   101  
   102  		return input
   103  	}
   104  
   105  	input_slice	:=	[]float64 { -1.1, 0, -2.1, 3.1 }
   106  	expected	:=	[]float64 {  1.1, 0,  2.1, 3.1 }
   107  	actual		:=	angols.CollectSliceOfFloat64(input_slice, fn)
   108  
   109  	if !angols.EqualSliceOfFloat64(expected, actual) {
   110  
   111  		t.Errorf("actual value '%v' does not equal expected value '%v'", actual, expected)
   112  	}
   113  
   114  	if !angols.EqualSlice(expected, actual) {
   115  
   116  		t.Errorf("actual value '%v' does not equal expected value '%v'", actual, expected)
   117  	}
   118  }
   119  
   120  func Test_CollectSliceOfString_1(t *testing.T) {
   121  
   122  	input_slice	:=	[]string { "abc", "", "def", "G" }
   123  	expected	:=	[]string { "ABC", "", "DEF", "G" }
   124  	actual		:=	angols.CollectSliceOfString(input_slice, func(input string) string { return strings.ToUpper(input) })
   125  
   126  	if !angols.EqualSliceOfString(expected, actual) {
   127  
   128  		t.Errorf("actual value '%v' does not equal expected value '%v'", actual, expected)
   129  	}
   130  
   131  	if !angols.EqualSlice(expected, actual) {
   132  
   133  		t.Errorf("actual value '%v' does not equal expected value '%v'", actual, expected)
   134  	}
   135  }
   136  
   137  
   138  // Benchmarks
   139  
   140  func Benchmark_equal_ints_by_EqualSliceOfInt(b *testing.B) {
   141  
   142  	fn			:=	func(index int) (int, error) { return index, nil }
   143  	ints_1, _	:=	angols.GenerateSliceOfInt(int_equal_size, fn)
   144  	ints_2, _	:=	angols.GenerateSliceOfInt(int_equal_size, fn)
   145  
   146  	for i := 0; i != int_equal_iterations; i++ {
   147  
   148  		_ = angols.EqualSliceOfInt(ints_1, ints_2)
   149  	}
   150  }
   151  
   152  func Benchmark_equal_ints_by_EqualSlice(b *testing.B) {
   153  
   154  	fn			:=	func(index int) (int, error) { return index, nil }
   155  	ints_1, _	:=	angols.GenerateSliceOfInt(int_equal_size, fn)
   156  	ints_2, _	:=	angols.GenerateSliceOfInt(int_equal_size, fn)
   157  
   158  	for i := 0; i != int_equal_iterations; i++ {
   159  
   160  		_ = angols.EqualSlice(ints_1, ints_2)
   161  	}
   162  }
   163  
   164  func Benchmark_equal_uints_by_EqualSlice(b *testing.B) {
   165  
   166  	fn			:=	func(index int) (uint, error) { return uint(index), nil }
   167  	ints_1, _	:=	angols.GenerateSliceOfUInt(int_equal_size, fn)
   168  	ints_2, _	:=	angols.GenerateSliceOfUInt(int_equal_size, fn)
   169  
   170  	for i := 0; i != int_equal_iterations; i++ {
   171  
   172  		_ = angols.EqualSlice(ints_1, ints_2)
   173  	}
   174  }
   175