github.com/mitranim/gg@v0.1.17/text_decode_test.go (about)

     1  package gg_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  	u "unsafe"
     7  
     8  	"github.com/mitranim/gg"
     9  	"github.com/mitranim/gg/gtest"
    10  )
    11  
    12  func TestParseTo(t *testing.T) {
    13  	defer gtest.Catch(t)
    14  
    15  	gtest.PanicStr(`unsupported kind interface`, func() { gg.ParseTo[any](``) })
    16  	gtest.PanicStr(`unsupported kind struct`, func() { gg.ParseTo[SomeModel](``) })
    17  	gtest.PanicStr(`unsupported kind slice`, func() { gg.ParseTo[[]string](``) })
    18  	gtest.PanicStr(`unsupported kind chan`, func() { gg.ParseTo[chan struct{}](``) })
    19  	gtest.PanicStr(`unsupported kind func`, func() { gg.ParseTo[func()](``) })
    20  	gtest.PanicStr(`unsupported kind uintptr`, func() { gg.ParseTo[uintptr](``) })
    21  	gtest.PanicStr(`unsupported kind unsafe.Pointer`, func() { gg.ParseTo[u.Pointer](``) })
    22  
    23  	gtest.PanicStr(`invalid syntax`, func() { gg.ParseTo[int](``) })
    24  	gtest.PanicStr(`invalid syntax`, func() { gg.ParseTo[*int](``) })
    25  
    26  	gtest.Equal(gg.ParseTo[string](``), ``)
    27  	gtest.Equal(gg.ParseTo[string](`str`), `str`)
    28  
    29  	gtest.Equal(gg.ParseTo[*string](``), gg.Ptr(``))
    30  	gtest.Equal(gg.ParseTo[*string](`str`), gg.Ptr(`str`))
    31  
    32  	gtest.Equal(gg.ParseTo[int](`0`), 0)
    33  	gtest.Equal(gg.ParseTo[int](`123`), 123)
    34  
    35  	gtest.Equal(gg.ParseTo[*int](`0`), gg.Ptr(0))
    36  	gtest.Equal(gg.ParseTo[*int](`123`), gg.Ptr(123))
    37  
    38  	gtest.Equal(
    39  		gg.ParseTo[time.Time](`1234-05-23T12:34:56Z`),
    40  		time.Date(1234, 5, 23, 12, 34, 56, 0, time.UTC),
    41  	)
    42  
    43  	gtest.Equal(
    44  		gg.ParseTo[*time.Time](`1234-05-23T12:34:56Z`),
    45  		gg.Ptr(time.Date(1234, 5, 23, 12, 34, 56, 0, time.UTC)),
    46  	)
    47  }
    48  
    49  func BenchmarkParseTo_int(b *testing.B) {
    50  	for ind := 0; ind < b.N; ind++ {
    51  		gg.Nop1(gg.ParseTo[int](`123`))
    52  	}
    53  }
    54  
    55  func BenchmarkParseTo_int_ptr(b *testing.B) {
    56  	for ind := 0; ind < b.N; ind++ {
    57  		gg.Nop1(gg.ParseTo[*int](`123`))
    58  	}
    59  }
    60  
    61  func BenchmarkParseTo_Parser(b *testing.B) {
    62  	for ind := 0; ind < b.N; ind++ {
    63  		gg.Nop1(gg.ParseTo[ParserStr](`863872f79b1d4cc9a45e8027a6ad66ad`))
    64  	}
    65  }
    66  
    67  func BenchmarkParseTo_Parser_ptr(b *testing.B) {
    68  	for ind := 0; ind < b.N; ind++ {
    69  		gg.Nop1(gg.ParseTo[*ParserStr](`863872f79b1d4cc9a45e8027a6ad66ad`))
    70  	}
    71  }
    72  
    73  func BenchmarkParseTo_Unmarshaler(b *testing.B) {
    74  	src := []byte(`863872f79b1d4cc9a45e8027a6ad66ad`)
    75  
    76  	for ind := 0; ind < b.N; ind++ {
    77  		gg.Nop1(gg.ParseTo[UnmarshalerBytes](src))
    78  	}
    79  }
    80  
    81  func BenchmarkParseTo_Unmarshaler_ptr(b *testing.B) {
    82  	src := []byte(`863872f79b1d4cc9a45e8027a6ad66ad`)
    83  
    84  	for ind := 0; ind < b.N; ind++ {
    85  		gg.Nop1(gg.ParseTo[*UnmarshalerBytes](src))
    86  	}
    87  }
    88  
    89  func BenchmarkParseTo_time_Time(b *testing.B) {
    90  	for ind := 0; ind < b.N; ind++ {
    91  		gg.Nop1(gg.ParseTo[time.Time](`1234-05-23T12:34:56Z`))
    92  	}
    93  }
    94  
    95  func BenchmarkParseTo_time_Time_ptr(b *testing.B) {
    96  	for ind := 0; ind < b.N; ind++ {
    97  		gg.Nop1(gg.ParseTo[*time.Time](`1234-05-23T12:34:56Z`))
    98  	}
    99  }
   100  
   101  func BenchmarkParse(b *testing.B) {
   102  	var val int
   103  
   104  	for ind := 0; ind < b.N; ind++ {
   105  		gg.Parse(`123`, &val)
   106  	}
   107  }