github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/runtime/basestring_test.go (about)

     1  // Copyright 2016 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package grumpy
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestNormalizeEncoding(t *testing.T) {
    22  	cases := []struct {
    23  		encoding string
    24  		want     string
    25  	}{
    26  		{"utf8", "utf8"},
    27  		{"UTF-16  ", "utf16"},
    28  		{"  __Ascii__", "ascii"},
    29  		{"utf@#(%*#(*%16  ", "utf16"},
    30  		{"", ""},
    31  	}
    32  	for _, cas := range cases {
    33  		if got := normalizeEncoding(cas.encoding); got != cas.want {
    34  			t.Errorf("normalizeEncoding(%q) = %q, want %q", cas.encoding, got, cas.want)
    35  		}
    36  	}
    37  }
    38  
    39  func BenchmarkEscapeRune(b *testing.B) {
    40  	b.Run("low values", func(b *testing.B) {
    41  		for i := 0; i < b.N; i++ {
    42  			escapeRune(0x10)
    43  		}
    44  	})
    45  
    46  	b.Run("mid values", func(b *testing.B) {
    47  		for i := 0; i < b.N; i++ {
    48  			escapeRune(0x200)
    49  		}
    50  	})
    51  
    52  	b.Run("high values", func(b *testing.B) {
    53  		for i := 0; i < b.N; i++ {
    54  			escapeRune(0x20000)
    55  		}
    56  	})
    57  }