Skip to content
Myr

bytesconv_test.go

This file

Path

internal/bytesconv/bytesconv_test.go

124 lines · 2.7 KiB · Go

Last changed by

refactor: for loop can be modernized using range over int (#4392)wanghaolong613before adoptione3118cc

History of this file

Take it

The walk stopped after 200 commits: lines older than cf32d2d point at it.

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 1–8why, one step back
1// Copyright 2020 Gin Core Team. All rights reserved.
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package bytesconv67import (8 "bytes"

refactor(internal/bytesconv): replace rand usage with crypto/rand and rand.Int63 (#4259)Namebefore adoption77d70e5

line 9why, one step back
9 cRand "crypto/rand"

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 10–15why, one step back
10 "math/rand"
11 "strings"12 "testing"13 "time"14)15

ci(golangci-lint): update configuration and fix lint issues (#4247)Flc゛before adoptionc4287b1

lines 16–19why, one step back
16var (
17 testString = "Albert Einstein: Logic will get you from A to B. Imagination will take you everywhere."18 testBytes = []byte(testString)19)

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 20–32why, one step back
20
21func rawBytesToStr(b []byte) string {22 return string(b)23}2425func rawStrToBytes(s string) []byte {26 return []byte(s)27}2829// go test -v3031func TestBytesToString(t *testing.T) {32 data := make([]byte, 1024)

refactor: for loop can be modernized using range over int (#4392)wanghaolong613before adoptione3118cc

line 33why, one step back
33 for range 100 {

refactor(internal/bytesconv): replace rand usage with crypto/rand and rand.Int63 (#4259)Namebefore adoption77d70e5

lines 34–37why, one step back
34 _, err := cRand.Read(data)
35 if err != nil {36 t.Fatal(err)37 }

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 38–43why, one step back
38 if rawBytesToStr(data) != BytesToString(data) {
39 t.Fatal("don't match")40 }41 }42}43

test(bytesconv): add tests for empty/nil cases (#4454)Miladbefore adoption583db59

lines 44–52why, one step back
44func TestBytesToStringEmpty(t *testing.T) {
45 if got := BytesToString([]byte{}); got != "" {46 t.Fatalf("BytesToString([]byte{}) = %q; want empty string", got)47 }48 if got := BytesToString(nil); got != "" {49 t.Fatalf("BytesToString(nil) = %q; want empty string", got)50 }51}52

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 53–59why, one step back
53const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
54const (55 letterIdxBits = 6 // 6 bits to represent a letter index56 letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits57 letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits58)59

refactor(internal/bytesconv): replace rand usage with crypto/rand and rand.Int63 (#4259)Namebefore adoption77d70e5

line 60why, one step back
60var src = rand.New(rand.NewSource(time.Now().UnixNano()))

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 61–81why, one step back
61
62func RandStringBytesMaskImprSrcSB(n int) string {63 sb := strings.Builder{}64 sb.Grow(n)65 // A src.Int63() generates 63 random bits, enough for letterIdxMax characters!66 for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {67 if remain == 0 {68 cache, remain = src.Int63(), letterIdxMax69 }70 if idx := int(cache & letterIdxMask); idx < len(letterBytes) {71 sb.WriteByte(letterBytes[idx])72 i--73 }74 cache >>= letterIdxBits75 remain--76 }7778 return sb.String()79}8081func TestStringToBytes(t *testing.T) {

refactor: for loop can be modernized using range over int (#4392)wanghaolong613before adoptione3118cc

line 82why, one step back
82 for range 100 {

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 83–86why, one step back
83 s := RandStringBytesMaskImprSrcSB(64)
84 if !bytes.Equal(rawStrToBytes(s), StringToBytes(s)) {85 t.Fatal("don't match")86 }

test(bytesconv): add tests for empty/nil cases (#4454)Miladbefore adoption583db59

lines 87–96why, one step back
87 }
88}8990func TestStringToBytesEmpty(t *testing.T) {91 b := StringToBytes("")92 if len(b) != 0 {93 t.Fatalf(`StringToBytes("") length = %d; want 0`, len(b))94 }95 if !bytes.Equal(b, []byte("")) {96 t.Fatalf(`StringToBytes("") = %v; want []byte("")`, b)

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 97–102why, one step back
97 }
98}99100// go test -v -run=none -bench=^BenchmarkBytesConv -benchmem=true101102func BenchmarkBytesConvBytesToStrRaw(b *testing.B) {

refactor: use b.Loop() to simplify the code and improve performance (#4389)reddaisyybefore adoption9968c4b

line 103why, one step back
103 for b.Loop() {

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 104–108why, one step back
104 rawBytesToStr(testBytes)
105 }106}107108func BenchmarkBytesConvBytesToStr(b *testing.B) {

refactor: use b.Loop() to simplify the code and improve performance (#4389)reddaisyybefore adoption9968c4b

line 109why, one step back
109 for b.Loop() {

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 110–114why, one step back
110 BytesToString(testBytes)
111 }112}113114func BenchmarkBytesConvStrToBytesRaw(b *testing.B) {

refactor: use b.Loop() to simplify the code and improve performance (#4389)reddaisyybefore adoption9968c4b

line 115why, one step back
115 for b.Loop() {

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 116–120why, one step back
116 rawStrToBytes(testString)
117 }118}119120func BenchmarkBytesConvStrToBytes(b *testing.B) {

refactor: use b.Loop() to simplify the code and improve performance (#4389)reddaisyybefore adoption9968c4b

line 121why, one step back
121 for b.Loop() {

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 122–124why, one step back
122 StringToBytes(testString)
123 }124}