github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/runtime/slice_test.go (about)

     1  // Copyright 2016 Google Inc. All Rights Reserved.
     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 grumpy
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestSliceCalcSize(t *testing.T) {
    22  	fun := wrapFuncForTest(func(f *Frame, s *Slice, numElems int) (*Object, *BaseException) {
    23  		start, stop, step, sliceLen, raised := s.calcSlice(f, numElems)
    24  		if raised != nil {
    25  			return nil, raised
    26  		}
    27  		return newTestTuple(start, stop, step, sliceLen).ToObject(), nil
    28  	})
    29  	cases := []invokeTestCase{
    30  		{args: wrapArgs(newTestSlice(4), 6), want: newTestTuple(0, 4, 1, 4).ToObject()},
    31  		{args: wrapArgs(newTestSlice(-8), 3), want: newTestTuple(0, 0, 1, 0).ToObject()},
    32  		{args: wrapArgs(newTestSlice(0, 10), 3), want: newTestTuple(0, 3, 1, 3).ToObject()},
    33  		{args: wrapArgs(newTestSlice(1, 2, newObject(ObjectType)), 0), wantExc: mustCreateException(TypeErrorType, errBadSliceIndex)},
    34  		{args: wrapArgs(newTestSlice(newObject(ObjectType)), 10), wantExc: mustCreateException(TypeErrorType, errBadSliceIndex)},
    35  		{args: wrapArgs(newTestSlice(newObject(ObjectType), 4), 10), wantExc: mustCreateException(TypeErrorType, errBadSliceIndex)},
    36  		{args: wrapArgs(newTestSlice(1.0, 4), 10), wantExc: mustCreateException(TypeErrorType, errBadSliceIndex)},
    37  		{args: wrapArgs(newTestSlice(1, 2, 0), 3), wantExc: mustCreateException(ValueErrorType, "slice step cannot be zero")},
    38  	}
    39  	for _, cas := range cases {
    40  		if err := runInvokeTestCase(fun, &cas); err != "" {
    41  			t.Error(err)
    42  		}
    43  	}
    44  }
    45  
    46  func TestSliceCompare(t *testing.T) {
    47  	cases := []invokeTestCase{
    48  		{args: wrapArgs(newTestSlice(None), newTestSlice(None)), want: compareAllResultEq},
    49  		{args: wrapArgs(newTestSlice(2), newTestSlice(1)), want: compareAllResultGT},
    50  		{args: wrapArgs(newTestSlice(1, 2), newTestSlice(1, 3)), want: compareAllResultLT},
    51  		{args: wrapArgs(newTestSlice(1, 2, 3), newTestSlice(1, 2)), want: compareAllResultGT},
    52  		{args: wrapArgs(None, newTestSlice(1, 2)), want: compareAllResultLT},
    53  	}
    54  	for _, cas := range cases {
    55  		if err := runInvokeTestCase(compareAll, &cas); err != "" {
    56  			t.Error(err)
    57  		}
    58  	}
    59  }
    60  
    61  func TestSliceNew(t *testing.T) {
    62  	cases := []invokeTestCase{
    63  		{args: nil, wantExc: mustCreateException(TypeErrorType, "'__new__' of 'object' requires 3 arguments")},
    64  		{args: wrapArgs(10), want: (&Slice{Object{typ: SliceType}, nil, NewInt(10).ToObject(), nil}).ToObject()},
    65  		{args: wrapArgs(1.2, "foo"), want: (&Slice{Object{typ: SliceType}, NewFloat(1.2).ToObject(), NewStr("foo").ToObject(), nil}).ToObject()},
    66  		{args: wrapArgs(None, None, true), want: (&Slice{Object{typ: SliceType}, None, None, True.ToObject()}).ToObject()},
    67  		{args: wrapArgs(1, 2, 3, 4), wantExc: mustCreateException(TypeErrorType, "'__new__' of 'object' requires 3 arguments")},
    68  	}
    69  	for _, cas := range cases {
    70  		if err := runInvokeTestCase(SliceType.ToObject(), &cas); err != "" {
    71  			t.Error(err)
    72  		}
    73  	}
    74  }
    75  
    76  func TestSliceStrRepr(t *testing.T) {
    77  	cases := []invokeTestCase{
    78  		{args: wrapArgs(newTestSlice(3.14)), want: NewStr("slice(None, 3.14, None)").ToObject()},
    79  		{args: wrapArgs(newTestSlice("foo", "bar")), want: NewStr("slice('foo', 'bar', None)").ToObject()},
    80  		{args: wrapArgs(newTestSlice(1, 2, 3)), want: NewStr("slice(1, 2, 3)").ToObject()},
    81  	}
    82  	for _, cas := range cases {
    83  		if err := runInvokeTestCase(wrapFuncForTest(ToStr), &cas); err != "" {
    84  			t.Error(err)
    85  		}
    86  		if err := runInvokeTestCase(wrapFuncForTest(Repr), &cas); err != "" {
    87  			t.Error(err)
    88  		}
    89  	}
    90  }
    91  
    92  func newTestSlice(args ...interface{}) *Object {
    93  	return mustNotRaise(SliceType.Call(NewRootFrame(), wrapArgs(args...), nil))
    94  }