github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/log/log_test.go (about)

     1  // Copyright 2016 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package log
     5  
     6  import (
     7  	"testing"
     8  )
     9  
    10  func init() {
    11  	EnableLogCaching(4, 20)
    12  }
    13  
    14  func TestCaching(t *testing.T) {
    15  	tests := []struct{ str, want string }{
    16  		{"", ""},
    17  		{"a", "a\n"},
    18  		{"bb", "a\nbb\n"},
    19  		{"ccc", "a\nbb\nccc\n"},
    20  		{"dddd", "a\nbb\nccc\ndddd\n"},
    21  		{"eeeee", "bb\nccc\ndddd\neeeee\n"},
    22  		{"ffffff", "ccc\ndddd\neeeee\nffffff\n"},
    23  		{"ggggggg", "eeeee\nffffff\nggggggg\n"},
    24  		{"hhhhhhhh", "ggggggg\nhhhhhhhh\n"},
    25  		{"jjjjjjjjjjjjjjjjjjjjjjjjj", "jjjjjjjjjjjjjjjjjjjjjjjjj\n"},
    26  	}
    27  	prependTime = false
    28  	for _, test := range tests {
    29  		Logf(1, test.str)
    30  		out := CachedLogOutput()
    31  		if out != test.want {
    32  			t.Fatalf("wrote: %v\nwant: %v\ngot: %v", test.str, test.want, out)
    33  		}
    34  	}
    35  }
    36  
    37  func TestLazy(t *testing.T) {
    38  	// Ensure that the format message is formatted lazily only when logging enabled.
    39  	Logf(1e6, "%v", noFormat{t})
    40  }
    41  
    42  type noFormat struct{ *testing.T }
    43  
    44  func (nf noFormat) String() string {
    45  	nf.T.Fatalf("must not be formatted")
    46  	return ""
    47  }