github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/utils/funcitr/funcitr_test.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package funcitr
    16  
    17  import (
    18  	"reflect"
    19  	"strconv"
    20  	"strings"
    21  	"testing"
    22  )
    23  
    24  func TestMapStrings(t *testing.T) {
    25  	inputs := []string{"this", "THAT", "The", "oThEr"}
    26  	outputs := MapStrings(inputs, strings.ToLower)
    27  
    28  	if !reflect.DeepEqual(outputs, []string{"this", "that", "the", "other"}) {
    29  		t.Error("Failed to map over strings")
    30  	}
    31  }
    32  
    33  func TestMapSlice(t *testing.T) {
    34  	inputs := []interface{}{2, 4, 6, 8}
    35  	outputs := MapSlice(inputs, func(item interface{}) interface{} {
    36  		n := item.(int)
    37  		return strconv.FormatInt(int64(n), 10)
    38  	})
    39  
    40  	if !reflect.DeepEqual(outputs, []interface{}{"2", "4", "6", "8"}) {
    41  		t.Error("Failed to map over strings")
    42  	}
    43  }
    44  
    45  func TestFilterStrings(t *testing.T) {
    46  	inputs := []string{"this", "THAT", "The", "oThEr"}
    47  	outputs := FilterStrings(inputs, func(s string) bool { return !strings.Contains(s, "T") })
    48  
    49  	if !reflect.DeepEqual(outputs, []string{"this"}) {
    50  		t.Error("Failed to filter strings")
    51  	}
    52  }
    53  
    54  func TestMapInts(t *testing.T) {
    55  	inputs32 := []int{2, 4, 6, 8}
    56  	inputs64 := []int64{2, 4, 6, 8}
    57  
    58  	outputs32 := MapInts(inputs32, func(x int) int {
    59  		return x * 2
    60  	})
    61  	outputs64 := MapInt64s(inputs64, func(x int64) int64 {
    62  		return x * 2
    63  	})
    64  
    65  	if !reflect.DeepEqual(outputs32, []int{4, 8, 12, 16}) {
    66  		t.Error("Failed to map over strings")
    67  	}
    68  
    69  	if !reflect.DeepEqual(outputs64, []int64{4, 8, 12, 16}) {
    70  		t.Error("Failed to map over strings")
    71  	}
    72  
    73  }
    74  
    75  func TestMapFloats(t *testing.T) {
    76  	inputs32 := []float32{2, 4, 6, 8}
    77  	inputs64 := []float64{2, 4, 6, 8}
    78  
    79  	outputs32 := MapFloat32s(inputs32, func(x float32) float32 {
    80  		return x * 2
    81  	})
    82  	outputs64 := MapFloat64s(inputs64, func(x float64) float64 {
    83  		return x * 2
    84  	})
    85  
    86  	if !reflect.DeepEqual(outputs32, []float32{4, 8, 12, 16}) {
    87  		t.Error("Failed to map over strings")
    88  	}
    89  
    90  	if !reflect.DeepEqual(outputs64, []float64{4, 8, 12, 16}) {
    91  		t.Error("Failed to map over strings")
    92  	}
    93  }
    94  
    95  func TestNilSlices(t *testing.T) {
    96  	str := MapStrings(nil, func(s string) string {
    97  		return ""
    98  	})
    99  	f32 := MapFloat32s(nil, func(f float32) float32 {
   100  		return 0.0
   101  	})
   102  	f64 := MapFloat64s(nil, func(f float64) float64 {
   103  		return 0.0
   104  	})
   105  	i := MapInts(nil, func(x int) int {
   106  		return 0
   107  	})
   108  	i64 := MapInt64s(nil, func(i int64) int64 {
   109  		return 0
   110  	})
   111  	sl := MapSlice(nil, func(item interface{}) interface{} {
   112  		return nil
   113  	})
   114  
   115  	if str != nil || f32 != nil || f64 != nil || i != nil || i64 != nil || sl != nil {
   116  		t.Error("bad mapping of nil input slice")
   117  	}
   118  }