github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/api/resource/amount_test.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package resource
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestInt64AmountAsInt64(t *testing.T) {
    24  	for _, test := range []struct {
    25  		value  int64
    26  		scale  Scale
    27  		result int64
    28  		ok     bool
    29  	}{
    30  		{100, 0, 100, true},
    31  		{100, 1, 1000, true},
    32  		{100, -5, 0, false},
    33  		{100, 100, 0, false},
    34  	} {
    35  		r, ok := int64Amount{value: test.value, scale: test.scale}.AsInt64()
    36  		if r != test.result {
    37  			t.Errorf("%v: unexpected result: %d", test, r)
    38  		}
    39  		if ok != test.ok {
    40  			t.Errorf("%v: unexpected ok: %t", test, ok)
    41  		}
    42  	}
    43  }
    44  
    45  func TestInt64AmountAdd(t *testing.T) {
    46  	for _, test := range []struct {
    47  		a, b, c int64Amount
    48  		ok      bool
    49  	}{
    50  		{int64Amount{value: 100, scale: 1}, int64Amount{value: 10, scale: 2}, int64Amount{value: 200, scale: 1}, true},
    51  		{int64Amount{value: 100, scale: 1}, int64Amount{value: 1, scale: 2}, int64Amount{value: 110, scale: 1}, true},
    52  		{int64Amount{value: 100, scale: 1}, int64Amount{value: 1, scale: 100}, int64Amount{value: 1, scale: 100}, false},
    53  		{int64Amount{value: -5, scale: 2}, int64Amount{value: 50, scale: 1}, int64Amount{value: 0, scale: 1}, true},
    54  		{int64Amount{value: -5, scale: 2}, int64Amount{value: 5, scale: 2}, int64Amount{value: 0, scale: 2}, true},
    55  
    56  		{int64Amount{value: mostPositive, scale: -1}, int64Amount{value: 1, scale: -1}, int64Amount{value: 0, scale: -1}, false},
    57  		{int64Amount{value: mostPositive, scale: -1}, int64Amount{value: 0, scale: -1}, int64Amount{value: mostPositive, scale: -1}, true},
    58  		{int64Amount{value: mostPositive / 10, scale: 1}, int64Amount{value: 10, scale: 0}, int64Amount{value: mostPositive, scale: -1}, false},
    59  	} {
    60  		c := test.a
    61  		ok := c.Add(test.b)
    62  		if ok != test.ok {
    63  			t.Errorf("%v: unexpected ok: %t", test, ok)
    64  		}
    65  		if ok {
    66  			if c != test.c {
    67  				t.Errorf("%v: unexpected result: %d", test, c)
    68  			}
    69  		} else {
    70  			if c != test.a {
    71  				t.Errorf("%v: overflow addition mutated source: %d", test, c)
    72  			}
    73  		}
    74  
    75  		// addition is commutative
    76  		c = test.b
    77  		if ok := c.Add(test.a); ok != test.ok {
    78  			t.Errorf("%v: unexpected ok: %t", test, ok)
    79  		}
    80  		if ok {
    81  			if c != test.c {
    82  				t.Errorf("%v: unexpected result: %d", test, c)
    83  			}
    84  		} else {
    85  			if c != test.b {
    86  				t.Errorf("%v: overflow addition mutated source: %d", test, c)
    87  			}
    88  		}
    89  	}
    90  }
    91  func TestInt64AsCanonicalString(t *testing.T) {
    92  	for _, test := range []struct {
    93  		value    int64
    94  		scale    Scale
    95  		result   string
    96  		exponent int32
    97  	}{
    98  		{100, 0, "100", 0},
    99  		{100, 1, "1", 3},
   100  		{100, -1, "10", 0},
   101  		{10800, -10, "1080", -9},
   102  	} {
   103  		r, exp := int64Amount{value: test.value, scale: test.scale}.AsCanonicalBytes(nil)
   104  		if string(r) != test.result {
   105  			t.Errorf("%v: unexpected result: %s", test, r)
   106  		}
   107  		if exp != test.exponent {
   108  			t.Errorf("%v: unexpected exponent: %d", test, exp)
   109  		}
   110  	}
   111  }
   112  
   113  func TestAmountSign(t *testing.T) {
   114  	table := []struct {
   115  		i      int64Amount
   116  		expect int
   117  	}{
   118  		{int64Amount{value: -50, scale: 1}, -1},
   119  		{int64Amount{value: 0, scale: 1}, 0},
   120  		{int64Amount{value: 300, scale: 1}, 1},
   121  		{int64Amount{value: -50, scale: -8}, -1},
   122  		{int64Amount{value: 50, scale: -8}, 1},
   123  		{int64Amount{value: 0, scale: -8}, 0},
   124  		{int64Amount{value: -50, scale: 0}, -1},
   125  		{int64Amount{value: 50, scale: 0}, 1},
   126  		{int64Amount{value: 0, scale: 0}, 0},
   127  	}
   128  	for _, testCase := range table {
   129  		if result := testCase.i.Sign(); result != testCase.expect {
   130  			t.Errorf("i: %v, Expected: %v, Actual: %v", testCase.i, testCase.expect, result)
   131  		}
   132  	}
   133  }
   134  
   135  func TestInt64AmountAsScaledInt64(t *testing.T) {
   136  	for _, test := range []struct {
   137  		name   string
   138  		i      int64Amount
   139  		scaled Scale
   140  		result int64
   141  		ok     bool
   142  	}{
   143  		{"test when i.scale < scaled ", int64Amount{value: 100, scale: 0}, 5, 1, true},
   144  		{"test when i.scale = scaled", int64Amount{value: 100, scale: 1}, 1, 100, true},
   145  		{"test when i.scale > scaled and result doesn't overflow", int64Amount{value: 100, scale: 5}, 2, 100000, true},
   146  		{"test when i.scale > scaled and result overflows", int64Amount{value: 876, scale: 30}, 4, 0, false},
   147  		{"test when i.scale < 0 and fraction exists", int64Amount{value: 93, scale: -1}, 0, 10, true},
   148  		{"test when i.scale < 0 and fraction doesn't exist", int64Amount{value: 100, scale: -1}, 0, 10, true},
   149  		{"test when i.value < 0 and fraction exists", int64Amount{value: -1932, scale: 2}, 4, -20, true},
   150  		{"test when i.value < 0 and fraction doesn't exists", int64Amount{value: -1900, scale: 2}, 4, -19, true},
   151  	} {
   152  		t.Run(test.name, func(t *testing.T) {
   153  			r, ok := test.i.AsScaledInt64(test.scaled)
   154  			if r != test.result {
   155  				t.Errorf("%v: expected result: %d, got result: %d", test.name, test.result, r)
   156  			}
   157  			if ok != test.ok {
   158  				t.Errorf("%v: expected ok: %t, got ok: %t", test.name, test.ok, ok)
   159  			}
   160  		})
   161  	}
   162  }