github.com/dubbogo/gost@v1.14.0/math/big/integer_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package gxbig
    19  
    20  import (
    21  	"encoding/json"
    22  	"reflect"
    23  	"testing"
    24  )
    25  
    26  func TestInteger(t *testing.T) {
    27  	tests := []struct {
    28  		name       string
    29  		src        string
    30  		wantString string
    31  		wantErr    bool
    32  	}{
    33  		{`ten`, `10`, `10`, false},
    34  		{`-ten`, `-10`, `-10`, false},
    35  		{`30digits`, `123456789012345678901234567890`, `123456789012345678901234567890`, false},
    36  		{`-37digits`, `-3987683987354747618711421180841033728`, `-3987683987354747618711421180841033728`, false},
    37  		{`1x2x3x4`, `79228162551157825753847955460`, `79228162551157825753847955460`, false},
    38  		{`invalid-x010`, `x010`, ``, true},
    39  		{`invalid-a010`, `a010`, ``, true},
    40  		{`invalid-10x`, `10x`, ``, true},
    41  		{`invalid-010x`, `010x`, ``, true},
    42  		{`special-010`, `010`, `10`, false},
    43  	}
    44  	for _, tt := range tests {
    45  		t.Run(tt.name, func(t *testing.T) {
    46  			i := &Integer{}
    47  			err := i.FromString(tt.src)
    48  			if (err != nil) != tt.wantErr {
    49  				t.Errorf("FromString() error = %v, wantErr %v", err, tt.wantErr)
    50  				return
    51  			}
    52  
    53  			if err == nil && i.String() != tt.wantString {
    54  				t.Errorf("String() got %v, want %v", i.String(), tt.wantString)
    55  			}
    56  		})
    57  	}
    58  }
    59  
    60  func TestInteger_FromSignAndMag(t *testing.T) {
    61  	type args struct {
    62  		signum int32
    63  		mag    []int
    64  	}
    65  	tests := []struct {
    66  		name  string
    67  		digit string
    68  		args  args
    69  	}{
    70  		{`0`, `0`, args{0, []int{}}},
    71  		{`1`, `1`, args{1, []int{1}}},
    72  		{`2147483647`, `2147483647`, args{1, []int{2147483647}}},
    73  		{`4294967295`, `4294967295`, args{1, []int{4294967295}}},
    74  		{`4294967296`, `4294967296`, args{1, []int{1, 0}}},
    75  		{`4294967298`, `4294967298`, args{1, []int{1, 2}}},
    76  		// these case is build by python
    77  		{`1x2x3`, `18446744082299486211`, args{1, []int{1, 2, 3}}},
    78  		{`1x2x3x4`, `79228162551157825753847955460`, args{1, []int{1, 2, 3, 4}}},
    79  		{`(((1<<32)-1)<<32)|(1<<10)`, `18446744069414585344`, args{1, []int{1<<32 - 1, 1 << 10}}},
    80  		{`(((1<<24)-1)<<32)|(1<<10)`, `72057589742961664`, args{1, []int{1<<24 - 1, 1 << 10}}},
    81  		{`(((1<<16)-1)<<32)|(1<<10)`, `281470681744384`, args{1, []int{1<<16 - 1, 0x400}}},
    82  		{`(((1<<8)-1)<<32)|(1<<10)`, `1095216661504`, args{1, []int{0xFF, 0x400}}},
    83  		{`maxuint64=(1<<64)-1`, `18446744073709551615`, args{1, []int{1<<32 - 1, 0xFFFFFFFF}}},
    84  		{`(1<<64)`, `18446744073709551616`, args{1, []int{1, 0, 0}}},
    85  		{`0x123456781234567812345678`, `5634002657842756053938493048`, args{1, []int{0x12345678, 0x12345678, 0x12345678}}},
    86  		{`-1`, `-1`, args{-1, []int{1}}},
    87  		{`-4294967296`, `-4294967296`, args{-1, []int{1, 0}}},
    88  		{`-4294967298`, `-4294967298`, args{-1, []int{1, 2}}},
    89  		{`-1x2x3`, `-18446744082299486211`, args{-1, []int{1, 2, 3}}},
    90  	}
    91  	for _, tt := range tests {
    92  		t.Run(tt.name, func(t *testing.T) {
    93  			i := &Integer{}
    94  			i.FromSignAndMag(tt.args.signum, tt.args.mag)
    95  			if i.String() != tt.digit {
    96  				t.Errorf("digit %s got = %s", tt.digit, i.String())
    97  			}
    98  
    99  			s := &Integer{}
   100  			err := s.FromString(tt.digit)
   101  			if err != nil {
   102  				t.Error("FromString error = ", err)
   103  			}
   104  
   105  			sign, mag := s.GetSignAndMag()
   106  			if !(sign == tt.args.signum && reflect.DeepEqual(mag, tt.args.mag)) {
   107  				t.Error("want ", tt.args.signum, tt.args.mag,
   108  					"got", sign, mag)
   109  			}
   110  		})
   111  	}
   112  }
   113  
   114  func TestInteger_Json(t *testing.T) {
   115  	i := new(Integer)
   116  	i.FromString(`1234567`)
   117  	bytes, err := json.Marshal(i.String())
   118  	if err != nil || string(bytes) != `"1234567"` {
   119  		t.Error(string(bytes), err)
   120  	}
   121  
   122  	err = json.Unmarshal([]byte(`345`), i)
   123  	if err != nil || i.String() != `345` {
   124  		t.Error(i, err)
   125  	}
   126  }