github.com/theQRL/go-zond@v0.1.1/log/format_test.go (about)

     1  package log
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"math/big"
     7  	"math/rand"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/holiman/uint256"
    12  )
    13  
    14  func TestPrettyInt64(t *testing.T) {
    15  	tests := []struct {
    16  		n int64
    17  		s string
    18  	}{
    19  		{0, "0"},
    20  		{10, "10"},
    21  		{-10, "-10"},
    22  		{100, "100"},
    23  		{-100, "-100"},
    24  		{1000, "1000"},
    25  		{-1000, "-1000"},
    26  		{10000, "10000"},
    27  		{-10000, "-10000"},
    28  		{99999, "99999"},
    29  		{-99999, "-99999"},
    30  		{100000, "100,000"},
    31  		{-100000, "-100,000"},
    32  		{1000000, "1,000,000"},
    33  		{-1000000, "-1,000,000"},
    34  		{math.MaxInt64, "9,223,372,036,854,775,807"},
    35  		{math.MinInt64, "-9,223,372,036,854,775,808"},
    36  	}
    37  	for i, tt := range tests {
    38  		if have := FormatLogfmtInt64(tt.n); have != tt.s {
    39  			t.Errorf("test %d: format mismatch: have %s, want %s", i, have, tt.s)
    40  		}
    41  	}
    42  }
    43  
    44  func TestPrettyUint64(t *testing.T) {
    45  	tests := []struct {
    46  		n uint64
    47  		s string
    48  	}{
    49  		{0, "0"},
    50  		{10, "10"},
    51  		{100, "100"},
    52  		{1000, "1000"},
    53  		{10000, "10000"},
    54  		{99999, "99999"},
    55  		{100000, "100,000"},
    56  		{1000000, "1,000,000"},
    57  		{math.MaxUint64, "18,446,744,073,709,551,615"},
    58  	}
    59  	for i, tt := range tests {
    60  		if have := FormatLogfmtUint64(tt.n); have != tt.s {
    61  			t.Errorf("test %d: format mismatch: have %s, want %s", i, have, tt.s)
    62  		}
    63  	}
    64  }
    65  
    66  func TestPrettyBigInt(t *testing.T) {
    67  	tests := []struct {
    68  		int string
    69  		s   string
    70  	}{
    71  		{"111222333444555678999", "111,222,333,444,555,678,999"},
    72  		{"-111222333444555678999", "-111,222,333,444,555,678,999"},
    73  		{"11122233344455567899900", "11,122,233,344,455,567,899,900"},
    74  		{"-11122233344455567899900", "-11,122,233,344,455,567,899,900"},
    75  	}
    76  
    77  	for _, tt := range tests {
    78  		v, _ := new(big.Int).SetString(tt.int, 10)
    79  		if have := formatLogfmtBigInt(v); have != tt.s {
    80  			t.Errorf("invalid output %s, want %s", have, tt.s)
    81  		}
    82  	}
    83  }
    84  
    85  func TestPrettyUint256(t *testing.T) {
    86  	tests := []struct {
    87  		int string
    88  		s   string
    89  	}{
    90  		{"111222333444555678999", "111,222,333,444,555,678,999"},
    91  		{"11122233344455567899900", "11,122,233,344,455,567,899,900"},
    92  	}
    93  
    94  	for _, tt := range tests {
    95  		v := new(uint256.Int)
    96  		v.SetFromDecimal(tt.int)
    97  		if have := formatLogfmtUint256(v); have != tt.s {
    98  			t.Errorf("invalid output %s, want %s", have, tt.s)
    99  		}
   100  	}
   101  }
   102  
   103  var sink string
   104  
   105  func BenchmarkPrettyInt64Logfmt(b *testing.B) {
   106  	b.ReportAllocs()
   107  	for i := 0; i < b.N; i++ {
   108  		sink = FormatLogfmtInt64(rand.Int63())
   109  	}
   110  }
   111  
   112  func BenchmarkPrettyUint64Logfmt(b *testing.B) {
   113  	b.ReportAllocs()
   114  	for i := 0; i < b.N; i++ {
   115  		sink = FormatLogfmtUint64(rand.Uint64())
   116  	}
   117  }
   118  
   119  func TestSanitation(t *testing.T) {
   120  	msg := "\u001b[1G\u001b[K\u001b[1A"
   121  	msg2 := "\u001b  \u0000"
   122  	msg3 := "NiceMessage"
   123  	msg4 := "Space Message"
   124  	msg5 := "Enter\nMessage"
   125  
   126  	for i, tt := range []struct {
   127  		msg  string
   128  		want string
   129  	}{
   130  		{
   131  			msg:  msg,
   132  			want: fmt.Sprintf("] %q                   %q=%q\n", msg, msg, msg),
   133  		},
   134  		{
   135  			msg:  msg2,
   136  			want: fmt.Sprintf("] %q                             %q=%q\n", msg2, msg2, msg2),
   137  		},
   138  		{
   139  			msg:  msg3,
   140  			want: fmt.Sprintf("] %s                              %s=%s\n", msg3, msg3, msg3),
   141  		},
   142  		{
   143  			msg:  msg4,
   144  			want: fmt.Sprintf("] %s                            %q=%q\n", msg4, msg4, msg4),
   145  		},
   146  		{
   147  			msg:  msg5,
   148  			want: fmt.Sprintf("] %s                            %q=%q\n", msg5, msg5, msg5),
   149  		},
   150  	} {
   151  		var (
   152  			logger = New()
   153  			out    = new(strings.Builder)
   154  		)
   155  		logger.SetHandler(LvlFilterHandler(LvlInfo, StreamHandler(out, TerminalFormat(false))))
   156  		logger.Info(tt.msg, tt.msg, tt.msg)
   157  		if have := out.String()[24:]; tt.want != have {
   158  			t.Fatalf("test %d: want / have: \n%v\n%v", i, tt.want, have)
   159  		}
   160  	}
   161  }