github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/api/resource/quantity_proto_test.go (about) 1 /* 2 Copyright 2017 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 inf "gopkg.in/inf.v0" 23 ) 24 25 func TestQuantityProtoMarshal(t *testing.T) { 26 // Test when d is nil 27 table := []struct { 28 quantity string 29 expect Quantity 30 }{ 31 {"0", Quantity{i: int64Amount{value: 0, scale: 0}, s: "0", Format: DecimalSI}}, 32 {"100m", Quantity{i: int64Amount{value: 100, scale: -3}, s: "100m", Format: DecimalSI}}, 33 {"50m", Quantity{i: int64Amount{value: 50, scale: -3}, s: "50m", Format: DecimalSI}}, 34 {"10000T", Quantity{i: int64Amount{value: 10000, scale: 12}, s: "10000T", Format: DecimalSI}}, 35 } 36 for _, testCase := range table { 37 q := MustParse(testCase.quantity) 38 // Won't currently get an error as MarshalTo can't return one 39 result, _ := q.Marshal() 40 q.MarshalTo(result) 41 if q.Cmp(testCase.expect) != 0 { 42 t.Errorf("Expected: %v, Actual: %v", testCase.expect, q) 43 } 44 } 45 // Test when i is {0,0} 46 table2 := []struct { 47 dec *inf.Dec 48 expect Quantity 49 }{ 50 {dec(0, 0).Dec, Quantity{i: int64Amount{value: 0, scale: 0}, d: infDecAmount{dec(0, 0).Dec}, s: "0", Format: DecimalSI}}, 51 {dec(10, 0).Dec, Quantity{i: int64Amount{value: 0, scale: 0}, d: infDecAmount{dec(10, 0).Dec}, s: "10", Format: DecimalSI}}, 52 {dec(-10, 0).Dec, Quantity{i: int64Amount{value: 0, scale: 0}, d: infDecAmount{dec(-10, 0).Dec}, s: "-10", Format: DecimalSI}}, 53 } 54 for _, testCase := range table2 { 55 q := Quantity{d: infDecAmount{testCase.dec}, Format: DecimalSI} 56 // Won't currently get an error as MarshalTo can't return one 57 result, _ := q.Marshal() 58 q.Unmarshal(result) 59 if q.Cmp(testCase.expect) != 0 { 60 t.Errorf("Expected: %v, Actual: %v", testCase.expect, q) 61 } 62 } 63 } 64 65 func TestQuantityProtoUnmarshal(t *testing.T) { 66 // Test when d is nil 67 table := []struct { 68 input Quantity 69 expect string 70 }{ 71 {Quantity{i: int64Amount{value: 0, scale: 0}, s: "0", Format: DecimalSI}, "0"}, 72 {Quantity{i: int64Amount{value: 100, scale: -3}, s: "100m", Format: DecimalSI}, "100m"}, 73 {Quantity{i: int64Amount{value: 50, scale: -3}, s: "50m", Format: DecimalSI}, "50m"}, 74 {Quantity{i: int64Amount{value: 10000, scale: 12}, s: "10000T", Format: DecimalSI}, "10000T"}, 75 } 76 for _, testCase := range table { 77 var inputQ Quantity 78 expectQ := MustParse(testCase.expect) 79 inputByteArray, _ := testCase.input.Marshal() 80 inputQ.Unmarshal(inputByteArray) 81 if inputQ.Cmp(expectQ) != 0 { 82 t.Errorf("Expected: %v, Actual: %v", inputQ, expectQ) 83 } 84 } 85 // Test when i is {0,0} 86 table2 := []struct { 87 input Quantity 88 expect *inf.Dec 89 }{ 90 {Quantity{i: int64Amount{value: 0, scale: 0}, d: infDecAmount{dec(0, 0).Dec}, s: "0", Format: DecimalSI}, dec(0, 0).Dec}, 91 {Quantity{i: int64Amount{value: 0, scale: 0}, d: infDecAmount{dec(10, 0).Dec}, s: "10", Format: DecimalSI}, dec(10, 0).Dec}, 92 {Quantity{i: int64Amount{value: 0, scale: 0}, d: infDecAmount{dec(-10, 0).Dec}, s: "-10", Format: DecimalSI}, dec(-10, 0).Dec}, 93 } 94 for _, testCase := range table2 { 95 var inputQ Quantity 96 expectQ := Quantity{d: infDecAmount{testCase.expect}, Format: DecimalSI} 97 inputByteArray, _ := testCase.input.Marshal() 98 inputQ.Unmarshal(inputByteArray) 99 if inputQ.Cmp(expectQ) != 0 { 100 t.Errorf("Expected: %v, Actual: %v", inputQ, expectQ) 101 } 102 } 103 }