github.com/grailbio/bigslice@v0.0.0-20230519005545-30c4c12152ad/typecheck/typecheck_test.go (about)

     1  // Copyright 2018 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache 2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  package typecheck
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  
    11  	"github.com/grailbio/bigslice/slicetype"
    12  )
    13  
    14  var (
    15  	typeOfString = reflect.TypeOf("")
    16  	typeOfInt    = reflect.TypeOf(0)
    17  	typeOfBool   = reflect.TypeOf(false)
    18  )
    19  
    20  func TestEqual(t *testing.T) {
    21  	for _, c := range []struct{ t1, t2 slicetype.Type }{
    22  		{slicetype.New(typeOfString), slicetype.New()},
    23  		{slicetype.New(typeOfInt, typeOfString), slicetype.New(typeOfString, typeOfInt)},
    24  		{slicetype.New(typeOfInt), slicetype.New(typeOfInt, typeOfInt)},
    25  		{slicetype.New(typeOfString), slicetype.New(typeOfInt)},
    26  	} {
    27  		if Equal(c.t1, c.t2) {
    28  			t.Errorf("types %s and %s are equal", slicetype.String(c.t1), slicetype.String(c.t2))
    29  		}
    30  	}
    31  	for _, typ := range []slicetype.Type{
    32  		slicetype.New(typeOfString),
    33  		slicetype.New(typeOfInt, typeOfString),
    34  		slicetype.New(),
    35  	} {
    36  		if !Equal(typ, typ) {
    37  			t.Errorf("type %s not equal to itself", slicetype.String(typ))
    38  		}
    39  	}
    40  }
    41  
    42  func TestSlices(t *testing.T) {
    43  	typ, ok := Slices([]int{1, 2, 3}, []string{"a", "b", "c"})
    44  	if !ok {
    45  		t.Fatal("!ok")
    46  	}
    47  	if got, want := typ.NumOut(), 2; got != want {
    48  		t.Fatalf("got %v, want %v", got, want)
    49  	}
    50  	if got, want := typ.Out(0), typeOfInt; got != want {
    51  		t.Errorf("got %v, want %v", got, want)
    52  	}
    53  	if got, want := typ.Out(1), typeOfString; got != want {
    54  		t.Errorf("got %v, want %v", got, want)
    55  	}
    56  
    57  	_, ok = Slices([]int{1, 2, 3}, "ok")
    58  	if ok {
    59  		t.Error("ok")
    60  	}
    61  }
    62  
    63  func TestDevectorize(t *testing.T) {
    64  	tvec := slicetype.New(reflect.SliceOf(typeOfString), reflect.SliceOf(typeOfInt))
    65  	tunvec, ok := Devectorize(tvec)
    66  	if !ok {
    67  		t.Fatal("!ok")
    68  	}
    69  	if got, want := tunvec.NumOut(), 2; got != want {
    70  		t.Fatalf("got %v, want %v", got, want)
    71  	}
    72  	if got, want := tunvec.Out(0), typeOfString; got != want {
    73  		t.Errorf("got %v, want %v", got, want)
    74  	}
    75  	if got, want := tunvec.Out(1), typeOfInt; got != want {
    76  		t.Errorf("got %v, want %v", got, want)
    77  	}
    78  
    79  	tvec = slicetype.New(typeOfString)
    80  	_, ok = Devectorize(tvec)
    81  	if ok {
    82  		t.Error("ok")
    83  	}
    84  }