github.com/nikandfor/loc@v0.5.1/fmt_test.go (about) 1 package loc 2 3 import ( 4 "bytes" 5 "fmt" 6 "regexp" 7 "testing" 8 "unsafe" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestFmt(t *testing.T) { 14 t.Logf("[%d]", 1000) 15 t.Logf("[%6d]", 1000) 16 t.Logf("[%-6d]", 1000) 17 18 t.Logf("%v", Caller(0)) 19 } 20 21 func TestLocationFormat(t *testing.T) { 22 l := PC(0x1234cd) 23 24 // name, file, line := l.nameFileLine() 25 // t.Logf("location: %v %v %v", name, file, line) 26 27 SetCache(l, "github.com/nikandfor/loc.Caller", "github.com/nikandfor/loc/location.go", 26) 28 29 var b bytes.Buffer 30 31 fmt.Fprintf(&b, "%v", l) 32 assert.Equal(t, "location.go:26", b.String()) 33 34 b.Reset() 35 36 fmt.Fprintf(&b, "%.3v", l) 37 assert.Equal(t, "location.go: 26", b.String()) 38 39 b.Reset() 40 41 fmt.Fprintf(&b, "%18.3v", l) 42 assert.Equal(t, "location.go : 26", b.String()) 43 44 b.Reset() 45 46 fmt.Fprintf(&b, "%18.30v", l) 47 assert.Len(t, b.String(), 18) 48 49 b.Reset() 50 51 fmt.Fprintf(&b, "%10.1v", l) 52 assert.Equal(t, "locatio:26", b.String()) 53 54 b.Reset() 55 56 fmt.Fprintf(&b, "%18.1v", l) 57 assert.Len(t, b.String(), 18) 58 59 b.Reset() 60 61 fmt.Fprintf(&b, "%-18.3v", l) 62 assert.Equal(t, " location.go: 26", b.String()) 63 64 b.Reset() 65 66 fmt.Fprintf(&b, "%+v", l) 67 assert.True(t, regexp.MustCompile(`[\w./-]*location.go:26`).MatchString(b.String()), "got %v", b.String()) 68 69 b.Reset() 70 71 fmt.Fprintf(&b, "%n", l) 72 assert.Equal(t, "loc.Caller", b.String()) 73 74 b.Reset() 75 76 fmt.Fprintf(&b, "%12n", l) 77 assert.Equal(t, "loc.Caller ", b.String()) 78 79 b.Reset() 80 81 fmt.Fprintf(&b, "%-12s", l) 82 assert.Equal(t, " loc.Caller", b.String()) 83 84 b.Reset() 85 86 fmt.Fprintf(&b, "%f", l) 87 assert.Equal(t, "location.go", b.String()) 88 89 b.Reset() 90 91 fmt.Fprintf(&b, "%12f", l) 92 assert.Equal(t, "location.go ", b.String()) 93 94 b.Reset() 95 96 fmt.Fprintf(&b, "%d", l) 97 assert.Equal(t, "26", b.String()) 98 99 b.Reset() 100 101 fmt.Fprintf(&b, "%0100d", l) 102 assert.Len(t, b.String(), 20) 103 104 b.Reset() 105 106 fmt.Fprintf(&b, "%4l", l) 107 assert.Equal(t, " 26", b.String()) 108 109 b.Reset() 110 111 fmt.Fprintf(&b, "%x", l) 112 assert.Equal(t, "0x1234cd", b.String()) 113 114 b.Reset() 115 116 fmt.Fprintf(&b, "%8x", l) 117 assert.Equal(t, " 0x1234cd", b.String()) 118 119 b.Reset() 120 121 fmt.Fprintf(&b, "%010X", l) 122 assert.Equal(t, "0x00001234CD", b.String()) 123 124 b.Reset() 125 126 fmt.Fprintf(&b, "%010x", l) 127 assert.Equal(t, fmt.Sprintf("%010p", unsafe.Pointer(uintptr(l))), b.String()) 128 129 b.Reset() 130 131 fmt.Fprintf(&b, "%100x", l) 132 assert.Len(t, b.String(), 20) 133 } 134 135 func BenchmarkLocationString(b *testing.B) { 136 b.ReportAllocs() 137 138 l := Caller(0) 139 140 for i := 0; i < b.N; i++ { 141 _ = l.String() 142 } 143 } 144 145 func BenchmarkLocationFormat(b *testing.B) { 146 b.ReportAllocs() 147 148 var s formatter 149 s.flags['+'] = true 150 151 l := Caller(0) 152 153 for i := 0; i < b.N; i++ { 154 s.Reset() 155 156 l.Format(&s, 'v') 157 } 158 } 159 160 type formatter struct { 161 bytes.Buffer 162 flags [128]bool 163 prec int 164 width int 165 precok bool 166 widthok bool 167 } 168 169 func (f *formatter) Flag(c int) bool { 170 return f.flags[c] 171 } 172 173 func (f *formatter) Precision() (int, bool) { 174 return f.prec, f.precok 175 } 176 177 func (f *formatter) Width() (int, bool) { 178 return f.width, f.widthok 179 }