github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/test/conv_test.go (about)

     1  package test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/gramework/gramework"
     8  )
     9  
    10  func TestBytesToString(t *testing.T) {
    11  	tests := []struct {
    12  		slice []byte
    13  		want  string
    14  	}{
    15  		{
    16  			slice: []byte("Hello world"),
    17  			want:  "Hello world",
    18  		},
    19  		{
    20  			slice: []byte("Hello world2304823049823049823409238402394823094823049428304"),
    21  			want:  "Hello world2304823049823049823409238402394823094823049428304",
    22  		},
    23  	}
    24  	for _, tt := range tests {
    25  		if got := gramework.BytesToString(tt.slice); got != tt.want {
    26  			t.Errorf("BytesToString() = %v, want %v", got, tt.want)
    27  		}
    28  	}
    29  }
    30  
    31  func TestStringToBytes(t *testing.T) {
    32  	tests := []struct {
    33  		str  string
    34  		want []byte
    35  	}{
    36  		{
    37  			str:  "Hello world",
    38  			want: []byte("Hello world"),
    39  		},
    40  		{
    41  			str:  "Hello world2304823049823049823409238402394823094823049428304",
    42  			want: []byte("Hello world2304823049823049823409238402394823094823049428304"),
    43  		},
    44  	}
    45  	for _, tt := range tests {
    46  		if got := gramework.StringToBytes(tt.str); !bytes.Equal(got, tt.want) {
    47  			t.Errorf("BytesToString() = %v, want %v", got, tt.want)
    48  		}
    49  	}
    50  }
    51  
    52  func BenchmarkBytesToString(b *testing.B) {
    53  	s := []byte("Hello world2304823049823049823409238402394823094823049428304")
    54  	b.ResetTimer()
    55  	var a string
    56  	for i := 0; i < b.N; i++ {
    57  		a = gramework.BytesToString(s)
    58  	}
    59  	_ = a
    60  }
    61  
    62  func BenchmarkBytesToStringBuiltin(b *testing.B) {
    63  	s := []byte("Hello world2304823049823049823409238402394823094823049428304")
    64  	b.ResetTimer()
    65  	var a string
    66  	for i := 0; i < b.N; i++ {
    67  		a = string(s)
    68  	}
    69  	_ = a
    70  }
    71  
    72  func BenchmarkStringToBytes(b *testing.B) {
    73  	s := "Hello world2304823049823049823409238402394823094823049428304"
    74  	b.ResetTimer()
    75  	var a []byte
    76  	for i := 0; i < b.N; i++ {
    77  		a = gramework.StringToBytes(s)
    78  	}
    79  	_ = a
    80  }
    81  
    82  func BenchmarkStringToBytesBuiltin(b *testing.B) {
    83  	s := "Hello world2304823049823049823409238402394823094823049428304"
    84  	b.ResetTimer()
    85  	var a []byte
    86  	for i := 0; i < b.N; i++ {
    87  		a = []byte(s)
    88  	}
    89  	_ = a
    90  }