github.com/cnotch/ipchub@v1.1.0/utils/scan/scanner_test.go (about)

     1  // Copyright (c) 2019,CAOHONGJU All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package scan
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestScanner_Scan(t *testing.T) {
    14  	raw := "cao,hong,ju,ok"
    15  	t.Run("Scan", func(t *testing.T) {
    16  		advance, token, ok := Comma.Scan(raw)
    17  		assert.True(t, ok)
    18  		assert.Equal(t, "cao", token)
    19  		assert.Equal(t, "hong,ju,ok", advance)
    20  		i := 0
    21  		for ok {
    22  			advance, token, ok = Comma.Scan(advance)
    23  			if ok {
    24  				i++
    25  			}
    26  		}
    27  		assert.Equal(t, 2, i)
    28  		assert.Equal(t, "ok", token)
    29  	})
    30  }
    31  
    32  func Benchmark_Scanner_Scan(b *testing.B) {
    33  	s := `realm="Another Streaming Media", nonce="60a76a995a0cb012f1707abc188f60cb"`
    34  	b.ResetTimer()
    35  	b.RunParallel(func(pb *testing.PB) {
    36  		for pb.Next() {
    37  			realm := ""
    38  			nonce := ""
    39  			ok := true
    40  			advance := s
    41  			token := ""
    42  
    43  			for ok {
    44  				advance, token, ok = Comma.Scan(advance)
    45  				k, v, _ := EqualPair.Scan(token)
    46  				switch k {
    47  				case "realm":
    48  					realm = v
    49  				case "nonce":
    50  					nonce = v
    51  				}
    52  			}
    53  			_ = realm
    54  			_ = nonce
    55  		}
    56  	})
    57  }