github.com/qiaogw/arrgo@v0.0.8/shape_test.go (about)

     1  package arrgo
     2  
     3  import "testing"
     4  
     5  func TestArrf_ReShape(t *testing.T) {
     6  	arr := Array([]float64{1, 2, 3, 4, 5, 6}, 2, 3)
     7  	arr2 := arr.ReShape(3, 2)
     8  
     9  	if !SameIntSlice(arr.Strides, []int{6, 2, 1}) {
    10  		t.Error("Expected [6,2,1], got ", arr2.Strides)
    11  	}
    12  	if !SameIntSlice(arr.Shape, []int{3, 2}) {
    13  		t.Error("Expected [3, 2], got ", arr.Shape)
    14  	}
    15  	if !SameIntSlice(arr2.Shape, []int{3, 2}) {
    16  		t.Error("Expected [3, 2], got ", arr2.Shape)
    17  	}
    18  }
    19  
    20  func TestArrf_ReShapeException(t *testing.T) {
    21  	defer func() {
    22  		r := recover()
    23  		if r != SHAPE_ERROR {
    24  			t.Error("Expected Shape error, got ", r)
    25  		}
    26  	}()
    27  
    28  	Arange(4).ReShape(5)
    29  }
    30  
    31  func TestArrf_SameShapeTo(t *testing.T) {
    32  	a := Arange(4).ReShape(2, 2)
    33  	b := Array([]float64{3, 4, 5, 6}, 2, 2)
    34  	if a.SameShapeTo(b) != true {
    35  		t.Errorf("Expected true, got %t", a.SameShapeTo(b))
    36  	}
    37  }
    38  
    39  func TestVstack(t *testing.T) {
    40  	if Vstack() != nil {
    41  		t.Errorf("Expected nil, got %s", Vstack())
    42  	}
    43  
    44  	a := Arange(3)
    45  	stacked := Vstack(a)
    46  	if !stacked.Equal(Arange(3)).AllTrues() {
    47  		t.Errorf("Expected [0, 1, 2], got %s", stacked)
    48  	}
    49  
    50  	b := Array([]float64{3, 4, 5})
    51  	stacked = Vstack(a, b)
    52  	if !stacked.Equal(Array([]float64{0, 1, 2, 3, 4, 5}, 2, 3)).AllTrues() {
    53  		t.Errorf("Expected [[0 1 2] [3 4 5]], got %s", stacked)
    54  	}
    55  
    56  	a = Arange(2)
    57  	b = Arange(4).ReShape(2, 2)
    58  	stacked = Vstack(a, b)
    59  	if !stacked.Equal(Array([]float64{0, 1, 0, 1, 2, 3}, 3, 2)).AllTrues() {
    60  		t.Errorf("Expected [[0,1], [0,1], [2, 3]], got %s", stacked)
    61  	}
    62  }
    63  
    64  func TestVstackException(t *testing.T) {
    65  	a := Arange(4).ReShape(1, 2, 2)
    66  	defer func() {
    67  		r := recover()
    68  		if r != SHAPE_ERROR {
    69  			t.Errorf("Expected Shape error, got %s", r)
    70  		}
    71  	}()
    72  
    73  	Vstack(a)
    74  }
    75  
    76  func TestVstackException2(t *testing.T) {
    77  	a := Arange(4)
    78  	b := Arange(5)
    79  	defer func() {
    80  		r := recover()
    81  		if r != SHAPE_ERROR {
    82  			t.Error("Expected Shape error, got ", r)
    83  		}
    84  	}()
    85  
    86  	Vstack(a, b)
    87  }
    88  
    89  func TestHstack(t *testing.T) {
    90  	if Hstack() != nil {
    91  		t.Error("Expected nil, got ", Hstack())
    92  	}
    93  
    94  	a := Arange(3)
    95  	stacked := Hstack(a)
    96  	if !stacked.Equal(Arange(3)).AllTrues() {
    97  		t.Error("Expected [0, 1, 2], got ", stacked)
    98  	}
    99  	a = a.ReShape(3, 1)
   100  	b := Array([]float64{3, 4, 5}).ReShape(3, 1)
   101  	stacked = Hstack(a, b)
   102  	if !stacked.Equal(Array([]float64{0, 3, 1, 4, 2, 5}, 3, 2)).AllTrues() {
   103  		t.Error("Expected [[0 3] [1 4], [2 5]], got ", stacked)
   104  	}
   105  
   106  	a = Arange(2).ReShape(2, 1)
   107  	b = Arange(4).ReShape(2, 2)
   108  	stacked = Hstack(a, b)
   109  	if !stacked.Equal(Array([]float64{0, 0, 1, 1, 2, 3}, 2, 3)).AllTrues() {
   110  		t.Error("Expected [[0, 0, 1], [1, 2, 3]], got ", stacked)
   111  	}
   112  }
   113  
   114  func TestHstackException(t *testing.T) {
   115  	a := Arange(4).ReShape(1, 2, 2)
   116  	defer func() {
   117  		r := recover()
   118  		if r != SHAPE_ERROR {
   119  			t.Error("Expected Shape error, got ", r)
   120  		}
   121  	}()
   122  
   123  	Hstack(a)
   124  }
   125  
   126  func TestHstackException2(t *testing.T) {
   127  	a := Arange(4).ReShape(4, 1)
   128  	b := Arange(5).ReShape(5, 1)
   129  	defer func() {
   130  		r := recover()
   131  		if r != SHAPE_ERROR {
   132  			t.Error("Expected Shape error, got ", r)
   133  		}
   134  	}()
   135  
   136  	Hstack(a, b)
   137  }
   138  
   139  func TestConcat(t *testing.T) {
   140  	if Concat(0) != nil {
   141  		t.Error("Expected nil, got ", Concat(0))
   142  	}
   143  	concated := Concat(0, Arange(2))
   144  	if !concated.Equal(Arange(2)).AllTrues() {
   145  		t.Error("Expected [0, 1], got ", concated)
   146  	}
   147  
   148  	a := Arange(3)
   149  	b := Arange(1, 4)
   150  
   151  	concated = Concat(0, a, b)
   152  	if !concated.Equal(Array([]float64{0, 1, 2, 1, 2, 3}, 2, 3)).AllTrues() {
   153  		t.Error("Expected [[0,1,2], [1,2,3]], got ", concated)
   154  	}
   155  
   156  	a = Arange(3)
   157  	b = Arange(1, 4)
   158  
   159  	concated = Concat(1, a, b)
   160  	t.Log(concated)
   161  	if !concated.Equal(Array([]float64{0, 1, 2, 1, 2, 3}, 1, 6)).AllTrues() {
   162  		t.Error("Expected [[0,1,2,1,2,3]], got ", concated)
   163  	}
   164  
   165  }
   166  
   167  func TestConcatException(t *testing.T) {
   168  	a := Arange(4)
   169  	b := Arange(1, 4)
   170  
   171  	defer func() {
   172  		r := recover()
   173  		if r != SHAPE_ERROR {
   174  			t.Error("Expected Shape error, got ", r)
   175  		}
   176  	}()
   177  
   178  	Concat(0, a, b)
   179  }
   180  
   181  func TestConcatException2(t *testing.T) {
   182  	a := Arange(4)
   183  	b := Arange(1, 4)
   184  
   185  	defer func() {
   186  		r := recover()
   187  		if r != PARAMETER_ERROR {
   188  			t.Error("Expected PARAMETER_ERROR, got ", r)
   189  		}
   190  	}()
   191  
   192  	Concat(2, a, b)
   193  }
   194  
   195  func TestAtLeast2D(t *testing.T) {
   196  	a := Arange(10)
   197  	AtLeast2D(a)
   198  	if !SameIntSlice(a.Shape, []int{1, 10}) {
   199  		t.Error("Expected [1, 10], got ", a.Shape)
   200  	}
   201  
   202  	a.ReShape(1, 1, 10)
   203  	AtLeast2D(a)
   204  	if !SameIntSlice(a.Shape, []int{1, 1, 10}) {
   205  		t.Error("Expected [1, 1, 10], got ", a.Shape)
   206  	}
   207  
   208  	if AtLeast2D(nil) != nil {
   209  		t.Error("Expected nil, got ", AtLeast2D(nil))
   210  	}
   211  }
   212  
   213  func TestAtLeast2D2(t *testing.T) {
   214  	if AtLeast2D(nil) != nil {
   215  		t.Error("Expected nil, got ", AtLeast2D(nil))
   216  	}
   217  
   218  	arr := Arange(3)
   219  	AtLeast2D(arr)
   220  
   221  	if !SameIntSlice(arr.Shape, []int{1, 3}) {
   222  		t.Error("expected true, got false")
   223  	}
   224  
   225  	arr = Arange(3).ReShape(3, 1)
   226  	AtLeast2D(arr)
   227  
   228  	if !SameIntSlice(arr.Shape, []int{3, 1}) {
   229  		t.Error("expected true, got false")
   230  	}
   231  }
   232  
   233  func TestArrf_Flatten(t *testing.T) {
   234  	arr := Arange(3).ReShape(3, 1)
   235  	flattened := arr.Flatten()
   236  
   237  	if !flattened.SameShapeTo(Arange(3)) {
   238  		t.Error("expected [3], got ", flattened.Shape)
   239  	}
   240  }