github.com/cloudwego/hertz@v0.9.3/internal/bytesconv/bytesconv_64_test.go (about) 1 //go:build amd64 || arm64 || ppc64 2 // +build amd64 arm64 ppc64 3 4 /* 5 * Copyright 2022 CloudWeGo Authors 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 * The MIT License (MIT) 20 * 21 * Copyright (c) 2015-present Aliaksandr Valialkin, VertaMedia, Kirill Danshin, Erik Dubbelboer, FastHTTP Authors 22 * 23 * Permission is hereby granted, free of charge, to any person obtaining a copy 24 * of this software and associated documentation files (the "Software"), to deal 25 * in the Software without restriction, including without limitation the rights 26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 27 * copies of the Software, and to permit persons to whom the Software is 28 * furnished to do so, subject to the following conditions: 29 * 30 * The above copyright notice and this permission notice shall be included in 31 * all copies or substantial portions of the Software. 32 * 33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 39 * THE SOFTWARE. 40 * 41 * This file may have been modified by CloudWeGo authors. All CloudWeGo 42 * Modifications are Copyright 2022 CloudWeGo Authors. 43 */ 44 45 package bytesconv 46 47 import ( 48 "fmt" 49 "testing" 50 51 "github.com/cloudwego/hertz/pkg/common/test/assert" 52 ) 53 54 func TestWriteHexInt(t *testing.T) { 55 t.Parallel() 56 57 for _, v := range []struct { 58 s string 59 n int 60 }{ 61 {"0", 0}, 62 {"1", 1}, 63 {"123", 0x123}, 64 {"7fffffffffffffff", 0x7fffffffffffffff}, 65 } { 66 testWriteHexInt(t, v.n, v.s) 67 } 68 } 69 70 func TestReadHexInt(t *testing.T) { 71 t.Parallel() 72 73 for _, v := range []struct { 74 s string 75 n int 76 }{ 77 //errTooLargeHexNum "too large hex number" 78 //{"0123456789abcdef", -1}, 79 {"0", 0}, 80 {"fF", 0xff}, 81 {"00abc", 0xabc}, 82 {"7fffffff", 0x7fffffff}, 83 {"000", 0}, 84 {"1234ZZZ", 0x1234}, 85 {"7ffffffffffffff", 0x7ffffffffffffff}, 86 } { 87 testReadHexInt(t, v.s, v.n) 88 } 89 } 90 91 func TestParseUint(t *testing.T) { 92 t.Parallel() 93 94 for _, v := range []struct { 95 s string 96 i int 97 }{ 98 {"0", 0}, 99 {"123", 123}, 100 {"1234567890", 1234567890}, 101 {"123456789012345678", 123456789012345678}, 102 {"9223372036854775807", 9223372036854775807}, 103 } { 104 n, err := ParseUint(S2b(v.s)) 105 if err != nil { 106 t.Errorf("unexpected error: %v. s=%q n=%v", err, v.s, n) 107 } 108 assert.DeepEqual(t, n, v.i) 109 } 110 } 111 112 func TestParseUintError(t *testing.T) { 113 t.Parallel() 114 115 for _, v := range []struct { 116 s string 117 }{ 118 {""}, 119 {"cloudwego123"}, 120 {"1234.545"}, 121 {"-9223372036854775808"}, 122 {"9223372036854775808"}, 123 {"18446744073709551615"}, 124 } { 125 n, err := ParseUint(S2b(v.s)) 126 if err == nil { 127 t.Fatalf("Expecting error when parsing %q. obtained %d", v.s, n) 128 } 129 if n >= 0 { 130 t.Fatalf("Unexpected n=%d when parsing %q. Expected negative num", n, v.s) 131 } 132 } 133 } 134 135 func TestAppendUint(t *testing.T) { 136 t.Parallel() 137 138 for _, s := range []struct { 139 n int 140 }{ 141 {0}, 142 {123}, 143 {0x7fffffffffffffff}, 144 } { 145 expectedS := fmt.Sprintf("%d", s.n) 146 s := AppendUint(nil, s.n) 147 assert.DeepEqual(t, expectedS, B2s(s)) 148 } 149 }