Best Got code snippet using got.Read
int_test.go
Source:int_test.go
...37 dec.SetBytes(b)38 if !dec.Next() {39 t.Fatalf("unexpected next value: got true, exp false")40 }41 if v1 != dec.Read() {42 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), v1)43 }44}45func Test_IntegerEncoder_Two(t *testing.T) {46 enc := NewIntegerEncoder(2)47 var v1, v2 int64 = 1, 248 enc.Write(v1)49 enc.Write(v2)50 b, err := enc.Bytes()51 if err != nil {52 t.Fatalf("unexpected error: %v", err)53 }54 if got := b[0] >> 4; intCompressedSimple != got {55 t.Fatalf("encoding type mismatch: exp uncompressed, got %v", got)56 }57 var dec IntegerDecoder58 dec.SetBytes(b)59 if !dec.Next() {60 t.Fatalf("unexpected next value: got true, exp false")61 }62 if v1 != dec.Read() {63 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), v1)64 }65 if !dec.Next() {66 t.Fatalf("unexpected next value: got true, exp false")67 }68 if v2 != dec.Read() {69 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), v2)70 }71}72func Test_IntegerEncoder_Negative(t *testing.T) {73 enc := NewIntegerEncoder(3)74 var v1, v2, v3 int64 = -2, 0, 175 enc.Write(v1)76 enc.Write(v2)77 enc.Write(v3)78 b, err := enc.Bytes()79 if err != nil {80 t.Fatalf("unexpected error: %v", err)81 }82 if got := b[0] >> 4; intCompressedSimple != got {83 t.Fatalf("encoding type mismatch: exp uncompressed, got %v", got)84 }85 var dec IntegerDecoder86 dec.SetBytes(b)87 if !dec.Next() {88 t.Fatalf("unexpected next value: got true, exp false")89 }90 if v1 != dec.Read() {91 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), v1)92 }93 if !dec.Next() {94 t.Fatalf("unexpected next value: got true, exp false")95 }96 if v2 != dec.Read() {97 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), v2)98 }99 if !dec.Next() {100 t.Fatalf("unexpected next value: got true, exp false")101 }102 if v3 != dec.Read() {103 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), v3)104 }105}106func Test_IntegerEncoder_Large_Range(t *testing.T) {107 enc := NewIntegerEncoder(2)108 var v1, v2 int64 = math.MinInt64, math.MaxInt64109 enc.Write(v1)110 enc.Write(v2)111 b, err := enc.Bytes()112 if err != nil {113 t.Fatalf("unexpected error: %v", err)114 }115 if got := b[0] >> 4; intUncompressed != got {116 t.Fatalf("encoding type mismatch: exp uncompressed, got %v", got)117 }118 var dec IntegerDecoder119 dec.SetBytes(b)120 if !dec.Next() {121 t.Fatalf("unexpected next value: got true, exp false")122 }123 if v1 != dec.Read() {124 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), v1)125 }126 if !dec.Next() {127 t.Fatalf("unexpected next value: got true, exp false")128 }129 if v2 != dec.Read() {130 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), v2)131 }132}133func Test_IntegerEncoder_Uncompressed(t *testing.T) {134 enc := NewIntegerEncoder(3)135 var v1, v2, v3 int64 = 0, 1, 1 << 60136 enc.Write(v1)137 enc.Write(v2)138 enc.Write(v3)139 b, err := enc.Bytes()140 if err != nil {141 t.Fatalf("expected error: %v", err)142 }143 // 1 byte header + 3 * 8 byte values144 if exp := 25; len(b) != exp {145 t.Fatalf("length mismatch: got %v, exp %v", len(b), exp)146 }147 if got := b[0] >> 4; intUncompressed != got {148 t.Fatalf("encoding type mismatch: exp uncompressed, got %v", got)149 }150 var dec IntegerDecoder151 dec.SetBytes(b)152 if !dec.Next() {153 t.Fatalf("unexpected next value: got true, exp false")154 }155 if v1 != dec.Read() {156 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), v1)157 }158 if !dec.Next() {159 t.Fatalf("unexpected next value: got true, exp false")160 }161 if v2 != dec.Read() {162 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), v2)163 }164 if !dec.Next() {165 t.Fatalf("unexpected next value: got true, exp false")166 }167 if v3 != dec.Read() {168 t.Fatalf("read value mismatch: got %v, exp %v", dec.Read(), v3)169 }170}171func Test_IntegerEncoder_NegativeUncompressed(t *testing.T) {172 values := []int64{173 -2352281900722994752, 1438442655375607923, -4110452567888190110,174 -1221292455668011702, -1941700286034261841, -2836753127140407751,175 1432686216250034552, 3663244026151507025, -3068113732684750258,176 -1949953187327444488, 3713374280993588804, 3226153669854871355,177 -2093273755080502606, 1006087192578600616, -2272122301622271655,178 2533238229511593671, -4450454445568858273, 2647789901083530435,179 2761419461769776844, -1324397441074946198, -680758138988210958,180 94468846694902125, -2394093124890745254, -2682139311758778198,181 }182 enc := NewIntegerEncoder(256)183 for _, v := range values {184 enc.Write(v)185 }186 b, err := enc.Bytes()187 if err != nil {188 t.Fatalf("expected error: %v", err)189 }190 if got := b[0] >> 4; intUncompressed != got {191 t.Fatalf("encoding type mismatch: exp uncompressed, got %v", got)192 }193 var dec IntegerDecoder194 dec.SetBytes(b)195 i := 0196 for dec.Next() {197 if i > len(values) {198 t.Fatalf("read too many values: got %v, exp %v", i, len(values))199 }200 if values[i] != dec.Read() {201 t.Fatalf("read value %d mismatch: got %v, exp %v", i, dec.Read(), values[i])202 }203 i += 1204 }205 if i != len(values) {206 t.Fatalf("failed to read enough values: got %v, exp %v", i, len(values))207 }208}209func Test_IntegerEncoder_AllNegative(t *testing.T) {210 enc := NewIntegerEncoder(3)211 values := []int64{212 -10, -5, -1,213 }214 for _, v := range values {215 enc.Write(v)216 }217 b, err := enc.Bytes()218 if err != nil {219 t.Fatalf("unexpected error: %v", err)220 }221 if got := b[0] >> 4; intCompressedSimple != got {222 t.Fatalf("encoding type mismatch: exp uncompressed, got %v", got)223 }224 var dec IntegerDecoder225 dec.SetBytes(b)226 i := 0227 for dec.Next() {228 if i > len(values) {229 t.Fatalf("read too many values: got %v, exp %v", i, len(values))230 }231 if values[i] != dec.Read() {232 t.Fatalf("read value %d mismatch: got %v, exp %v", i, dec.Read(), values[i])233 }234 i += 1235 }236 if i != len(values) {237 t.Fatalf("failed to read enough values: got %v, exp %v", i, len(values))238 }239}240func Test_IntegerEncoder_CounterPacked(t *testing.T) {241 enc := NewIntegerEncoder(16)242 values := []int64{243 1e15, 1e15 + 1, 1e15 + 2, 1e15 + 3, 1e15 + 4, 1e15 + 6,244 }245 for _, v := range values {246 enc.Write(v)247 }248 b, err := enc.Bytes()249 if err != nil {250 t.Fatalf("unexpected error: %v", err)251 }252 if b[0]>>4 != intCompressedSimple {253 t.Fatalf("unexpected encoding format: expected simple, got %v", b[0]>>4)254 }255 // Should use 1 header byte + 2, 8 byte words if delta-encoding is used based on256 // values sizes. Without delta-encoding, we'd get 49 bytes.257 if exp := 17; len(b) != exp {258 t.Fatalf("encoded length mismatch: got %v, exp %v", len(b), exp)259 }260 var dec IntegerDecoder261 dec.SetBytes(b)262 i := 0263 for dec.Next() {264 if i > len(values) {265 t.Fatalf("read too many values: got %v, exp %v", i, len(values))266 }267 if values[i] != dec.Read() {268 t.Fatalf("read value %d mismatch: got %v, exp %v", i, dec.Read(), values[i])269 }270 i += 1271 }272 if i != len(values) {273 t.Fatalf("failed to read enough values: got %v, exp %v", i, len(values))274 }275}276func Test_IntegerEncoder_CounterRLE(t *testing.T) {277 enc := NewIntegerEncoder(16)278 values := []int64{279 1e15, 1e15 + 1, 1e15 + 2, 1e15 + 3, 1e15 + 4, 1e15 + 5,280 }281 for _, v := range values {282 enc.Write(v)283 }284 b, err := enc.Bytes()285 if err != nil {286 t.Fatalf("unexpected error: %v", err)287 }288 if b[0]>>4 != intCompressedRLE {289 t.Fatalf("unexpected encoding format: expected RLE, got %v", b[0]>>4)290 }291 // Should use 1 header byte, 8 byte first value, 1 var-byte for delta and 1 var-byte for292 // count of deltas in this particular RLE.293 if exp := 11; len(b) != exp {294 t.Fatalf("encoded length mismatch: got %v, exp %v", len(b), exp)295 }296 var dec IntegerDecoder297 dec.SetBytes(b)298 i := 0299 for dec.Next() {300 if i > len(values) {301 t.Fatalf("read too many values: got %v, exp %v", i, len(values))302 }303 if values[i] != dec.Read() {304 t.Fatalf("read value %d mismatch: got %v, exp %v", i, dec.Read(), values[i])305 }306 i += 1307 }308 if i != len(values) {309 t.Fatalf("failed to read enough values: got %v, exp %v", i, len(values))310 }311}312func Test_IntegerEncoder_Descending(t *testing.T) {313 enc := NewIntegerEncoder(16)314 values := []int64{315 7094, 4472, 1850,316 }317 for _, v := range values {318 enc.Write(v)319 }320 b, err := enc.Bytes()321 if err != nil {322 t.Fatalf("unexpected error: %v", err)323 }324 if b[0]>>4 != intCompressedRLE {325 t.Fatalf("unexpected encoding format: expected simple, got %v", b[0]>>4)326 }327 // Should use 1 header byte, 8 byte first value, 1 var-byte for delta and 1 var-byte for328 // count of deltas in this particular RLE.329 if exp := 12; len(b) != exp {330 t.Fatalf("encoded length mismatch: got %v, exp %v", len(b), exp)331 }332 var dec IntegerDecoder333 dec.SetBytes(b)334 i := 0335 for dec.Next() {336 if i > len(values) {337 t.Fatalf("read too many values: got %v, exp %v", i, len(values))338 }339 if values[i] != dec.Read() {340 t.Fatalf("read value %d mismatch: got %v, exp %v", i, dec.Read(), values[i])341 }342 i += 1343 }344 if i != len(values) {345 t.Fatalf("failed to read enough values: got %v, exp %v", i, len(values))346 }347}348func Test_IntegerEncoder_Flat(t *testing.T) {349 enc := NewIntegerEncoder(16)350 values := []int64{351 1, 1, 1, 1,352 }353 for _, v := range values {354 enc.Write(v)355 }356 b, err := enc.Bytes()357 if err != nil {358 t.Fatalf("unexpected error: %v", err)359 }360 if b[0]>>4 != intCompressedRLE {361 t.Fatalf("unexpected encoding format: expected simple, got %v", b[0]>>4)362 }363 // Should use 1 header byte, 8 byte first value, 1 var-byte for delta and 1 var-byte for364 // count of deltas in this particular RLE.365 if exp := 11; len(b) != exp {366 t.Fatalf("encoded length mismatch: got %v, exp %v", len(b), exp)367 }368 var dec IntegerDecoder369 dec.SetBytes(b)370 i := 0371 for dec.Next() {372 if i > len(values) {373 t.Fatalf("read too many values: got %v, exp %v", i, len(values))374 }375 if values[i] != dec.Read() {376 t.Fatalf("read value %d mismatch: got %v, exp %v", i, dec.Read(), values[i])377 }378 i += 1379 }380 if i != len(values) {381 t.Fatalf("failed to read enough values: got %v, exp %v", i, len(values))382 }383}384func Test_IntegerEncoder_MinMax(t *testing.T) {385 enc := NewIntegerEncoder(2)386 values := []int64{387 math.MinInt64, math.MaxInt64,388 }389 for _, v := range values {390 enc.Write(v)391 }392 b, err := enc.Bytes()393 if err != nil {394 t.Fatalf("unexpected error: %v", err)395 }396 if b[0]>>4 != intUncompressed {397 t.Fatalf("unexpected encoding format: expected simple, got %v", b[0]>>4)398 }399 if exp := 17; len(b) != exp {400 t.Fatalf("encoded length mismatch: got %v, exp %v", len(b), exp)401 }402 var dec IntegerDecoder403 dec.SetBytes(b)404 i := 0405 for dec.Next() {406 if i > len(values) {407 t.Fatalf("read too many values: got %v, exp %v", i, len(values))408 }409 if values[i] != dec.Read() {410 t.Fatalf("read value %d mismatch: got %v, exp %v", i, dec.Read(), values[i])411 }412 i += 1413 }414 if i != len(values) {415 t.Fatalf("failed to read enough values: got %v, exp %v", i, len(values))416 }417}418func Test_IntegerEncoder_Quick(t *testing.T) {419 quick.Check(func(values []int64) bool {420 if values == nil {421 values = []int64{} // is this really expected?422 }423 // Write values to encoder.424 enc := NewIntegerEncoder(1024)425 for _, v := range values {426 enc.Write(v)427 }428 // Retrieve encoded bytes from encoder.429 buf, err := enc.Bytes()430 if err != nil {431 t.Fatal(err)432 }433 // Read values out of decoder.434 got := make([]int64, 0, len(values))435 var dec IntegerDecoder436 dec.SetBytes(buf)437 for dec.Next() {438 if err := dec.Error(); err != nil {439 t.Fatal(err)440 }441 got = append(got, dec.Read().(int64))442 }443 // Verify that input and output values match.444 if !reflect.DeepEqual(values, got) {445 t.Fatalf("mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", values, got)446 }447 return true448 }, nil)449}450func Test_IntegerDecoder_Corrupt(t *testing.T) {451 cases := []string{452 "", // Empty453 "\x00abc", // Uncompressed: less than 8 bytes454 "\x10abc", // Packed: less than 8 bytes455 "\x20abc", // RLE: less than 8 bytes456 "\x2012345678\x90", // RLE: valid starting value but invalid delta value457 "\x2012345678\x01\x90", // RLE: valid starting, valid delta value, invalid repeat value458 }459 for _, c := range cases {460 var dec IntegerDecoder461 dec.SetBytes([]byte(c))462 if dec.Next() {463 t.Fatalf("exp next == false, got true")464 }465 }466}467func BenchmarkIntegerEncoderRLE(b *testing.B) {468 enc := NewIntegerEncoder(1024)469 x := make([]int64, 1024)470 for i := 0; i < len(x); i++ {471 x[i] = int64(i)472 enc.Write(x[i])473 }474 b.ResetTimer()475 for i := 0; i < b.N; i++ {476 enc.Bytes()477 }478}479func BenchmarkIntegerEncoderPackedSimple(b *testing.B) {480 enc := NewIntegerEncoder(1024)481 x := make([]int64, 1024)482 for i := 0; i < len(x); i++ {483 // Small amount of randomness prevents RLE from being used484 x[i] = int64(i) + int64(rand.Intn(10))485 enc.Write(x[i])486 }487 b.ResetTimer()488 for i := 0; i < b.N; i++ {489 enc.Bytes()490 enc.Reset()491 for i := 0; i < len(x); i++ {492 enc.Write(x[i])493 }494 }495}496func BenchmarkIntegerBatch_DecodeAllUncompressed(b *testing.B) {497 benchmarks := []struct {498 n int499 }{500 {5},501 {55},502 {555},503 {1000},504 }505 values := []int64{506 -2352281900722994752, 1438442655375607923, -4110452567888190110,507 -1221292455668011702, -1941700286034261841, -2836753127140407751,508 1432686216250034552, 3663244026151507025, -3068113732684750258,509 -1949953187327444488, 3713374280993588804, 3226153669854871355,510 -2093273755080502606, 1006087192578600616, -2272122301622271655,511 2533238229511593671, -4450454445568858273, 2647789901083530435,512 2761419461769776844, -1324397441074946198, -680758138988210958,513 94468846694902125, -2394093124890745254, -2682139311758778198,514 }515 for _, bm := range benchmarks {516 rand.Seed(int64(bm.n * 1e3))517 enc := NewIntegerEncoder(bm.n)518 for i := 0; i < bm.n; i++ {519 enc.Write(values[rand.Int()%len(values)])520 }521 bytes, _ := enc.Bytes()522 b.Run(fmt.Sprintf("%d", bm.n), func(b *testing.B) {523 b.SetBytes(int64(len(bytes)))524 b.ReportAllocs()525 dst := make([]int64, bm.n)526 for i := 0; i < b.N; i++ {527 var dec IntegerDecoder528 dec.SetBytes(bytes)529 var n int530 for dec.Next() {531 dst[n] = dec.Read().(int64)532 n++533 }534 }535 })536 }537}538func BenchmarkIntegerBatch_DecodeAllPackedSimple(b *testing.B) {539 benchmarks := []struct {540 n int541 }{542 {5},543 {55},544 {555},545 {1000},546 }547 for _, bm := range benchmarks {548 rand.Seed(int64(bm.n * 1e3))549 enc := NewIntegerEncoder(bm.n)550 for i := 0; i < bm.n; i++ {551 // Small amount of randomness prevents RLE from being used552 enc.Write(int64(i) + int64(rand.Intn(10)))553 }554 bytes, _ := enc.Bytes()555 b.Run(fmt.Sprintf("%d", bm.n), func(b *testing.B) {556 b.SetBytes(int64(len(bytes)))557 b.ReportAllocs()558 dst := make([]int64, bm.n)559 for i := 0; i < b.N; i++ {560 var dec IntegerDecoder561 dec.SetBytes(bytes)562 var n int563 for dec.Next() {564 dst[n] = dec.Read().(int64)565 n++566 }567 }568 })569 }570}571func BenchmarkIntegerBatch_DecodeAllRLE(b *testing.B) {572 benchmarks := []struct {573 n int574 delta int64575 }{576 {5, 1},577 {55, 1},578 {555, 1},579 {1000, 1},580 {1000, 0},581 }582 for _, bm := range benchmarks {583 enc := NewIntegerEncoder(bm.n)584 acc := int64(0)585 for i := 0; i < bm.n; i++ {586 enc.Write(acc)587 acc += bm.delta588 }589 bytes, _ := enc.Bytes()590 b.Run(fmt.Sprintf("%d_delta_%d", bm.n, bm.delta), func(b *testing.B) {591 b.SetBytes(int64(len(bytes)))592 b.ReportAllocs()593 dst := make([]int64, bm.n)594 for i := 0; i < b.N; i++ {595 var dec IntegerDecoder596 dec.SetBytes(bytes)597 var n int598 for dec.Next() {599 dst[n] = dec.Read().(int64)600 n++601 }602 }603 })604 }605}...
reader_test.go
Source:reader_test.go
...9 "io/ioutil"10 "sync"11 "testing"12)13func TestReader(t *testing.T) {14 r := NewReader([]byte("0123456789"))15 tests := []struct {16 off int6417 seek int18 n int19 want string20 wantpos int6421 readerr error22 seekerr string23 }{24 {seek: io.SeekStart, off: 0, n: 20, want: "0123456789"},25 {seek: io.SeekStart, off: 1, n: 1, want: "1"},26 {seek: io.SeekCurrent, off: 1, wantpos: 3, n: 2, want: "34"},27 {seek: io.SeekStart, off: -1, seekerr: "bytes.Reader.Seek: negative position"},28 {seek: io.SeekStart, off: 1 << 33, wantpos: 1 << 33, readerr: io.EOF},29 {seek: io.SeekCurrent, off: 1, wantpos: 1<<33 + 1, readerr: io.EOF},30 {seek: io.SeekStart, n: 5, want: "01234"},31 {seek: io.SeekCurrent, n: 5, want: "56789"},32 {seek: io.SeekEnd, off: -1, n: 1, wantpos: 9, want: "9"},33 }34 for i, tt := range tests {35 pos, err := r.Seek(tt.off, tt.seek)36 if err == nil && tt.seekerr != "" {37 t.Errorf("%d. want seek error %q", i, tt.seekerr)38 continue39 }40 if err != nil && err.Error() != tt.seekerr {41 t.Errorf("%d. seek error = %q; want %q", i, err.Error(), tt.seekerr)42 continue43 }44 if tt.wantpos != 0 && tt.wantpos != pos {45 t.Errorf("%d. pos = %d, want %d", i, pos, tt.wantpos)46 }47 buf := make([]byte, tt.n)48 n, err := r.Read(buf)49 if err != tt.readerr {50 t.Errorf("%d. read = %v; want %v", i, err, tt.readerr)51 continue52 }53 got := string(buf[:n])54 if got != tt.want {55 t.Errorf("%d. got %q; want %q", i, got, tt.want)56 }57 }58}59func TestReadAfterBigSeek(t *testing.T) {60 r := NewReader([]byte("0123456789"))61 if _, err := r.Seek(1<<31+5, io.SeekStart); err != nil {62 t.Fatal(err)63 }64 if n, err := r.Read(make([]byte, 10)); n != 0 || err != io.EOF {65 t.Errorf("Read = %d, %v; want 0, EOF", n, err)66 }67}68func TestReaderAt(t *testing.T) {69 r := NewReader([]byte("0123456789"))70 tests := []struct {71 off int6472 n int73 want string74 wanterr interface{}75 }{76 {0, 10, "0123456789", nil},77 {1, 10, "123456789", io.EOF},78 {1, 9, "123456789", nil},79 {11, 10, "", io.EOF},80 {0, 0, "", nil},81 {-1, 0, "", "bytes.Reader.ReadAt: negative offset"},82 }83 for i, tt := range tests {84 b := make([]byte, tt.n)85 rn, err := r.ReadAt(b, tt.off)86 got := string(b[:rn])87 if got != tt.want {88 t.Errorf("%d. got %q; want %q", i, got, tt.want)89 }90 if fmt.Sprintf("%v", err) != fmt.Sprintf("%v", tt.wanterr) {91 t.Errorf("%d. got error = %v; want %v", i, err, tt.wanterr)92 }93 }94}95func TestReaderAtConcurrent(t *testing.T) {96 // Test for the race detector, to verify ReadAt doesn't mutate97 // any state.98 r := NewReader([]byte("0123456789"))99 var wg sync.WaitGroup100 for i := 0; i < 5; i++ {101 wg.Add(1)102 go func(i int) {103 defer wg.Done()104 var buf [1]byte105 r.ReadAt(buf[:], int64(i))106 }(i)107 }108 wg.Wait()109}110func TestEmptyReaderConcurrent(t *testing.T) {111 // Test for the race detector, to verify a Read that doesn't yield any bytes112 // is okay to use from multiple goroutines. This was our historic behavior.113 // See golang.org/issue/7856114 r := NewReader([]byte{})115 var wg sync.WaitGroup116 for i := 0; i < 5; i++ {117 wg.Add(2)118 go func() {119 defer wg.Done()120 var buf [1]byte121 r.Read(buf[:])122 }()123 go func() {124 defer wg.Done()125 r.Read(nil)126 }()127 }128 wg.Wait()129}130func TestReaderWriteTo(t *testing.T) {131 for i := 0; i < 30; i += 3 {132 var l int133 if i > 0 {134 l = len(testString) / i135 }136 s := testString[:l]137 r := NewReader(testBytes[:l])138 var b Buffer139 n, err := r.WriteTo(&b)140 if expect := int64(len(s)); n != expect {141 t.Errorf("got %v; want %v", n, expect)142 }143 if err != nil {144 t.Errorf("for length %d: got error = %v; want nil", l, err)145 }146 if b.String() != s {147 t.Errorf("got string %q; want %q", b.String(), s)148 }149 if r.Len() != 0 {150 t.Errorf("reader contains %v bytes; want 0", r.Len())151 }152 }153}154func TestReaderLen(t *testing.T) {155 const data = "hello world"156 r := NewReader([]byte(data))157 if got, want := r.Len(), 11; got != want {158 t.Errorf("r.Len(): got %d, want %d", got, want)159 }160 if n, err := r.Read(make([]byte, 10)); err != nil || n != 10 {161 t.Errorf("Read failed: read %d %v", n, err)162 }163 if got, want := r.Len(), 1; got != want {164 t.Errorf("r.Len(): got %d, want %d", got, want)165 }166 if n, err := r.Read(make([]byte, 1)); err != nil || n != 1 {167 t.Errorf("Read failed: read %d %v; want 1, nil", n, err)168 }169 if got, want := r.Len(), 0; got != want {170 t.Errorf("r.Len(): got %d, want %d", got, want)171 }172}173var UnreadRuneErrorTests = []struct {174 name string175 f func(*Reader)176}{177 {"Read", func(r *Reader) { r.Read([]byte{0}) }},178 {"ReadByte", func(r *Reader) { r.ReadByte() }},179 {"UnreadRune", func(r *Reader) { r.UnreadRune() }},180 {"Seek", func(r *Reader) { r.Seek(0, io.SeekCurrent) }},181 {"WriteTo", func(r *Reader) { r.WriteTo(&Buffer{}) }},182}183func TestUnreadRuneError(t *testing.T) {184 for _, tt := range UnreadRuneErrorTests {185 reader := NewReader([]byte("0123456789"))186 if _, _, err := reader.ReadRune(); err != nil {187 // should not happen188 t.Fatal(err)189 }190 tt.f(reader)191 err := reader.UnreadRune()192 if err == nil {193 t.Errorf("Unreading after %s: expected error", tt.name)194 }195 }196}197func TestReaderDoubleUnreadRune(t *testing.T) {198 buf := NewBuffer([]byte("groucho"))199 if _, _, err := buf.ReadRune(); err != nil {200 // should not happen201 t.Fatal(err)202 }203 if err := buf.UnreadByte(); err != nil {204 // should not happen205 t.Fatal(err)206 }207 if err := buf.UnreadByte(); err == nil {208 t.Fatal("UnreadByte: expected error, got nil")209 }210}211// verify that copying from an empty reader always has the same results,212// regardless of the presence of a WriteTo method.213func TestReaderCopyNothing(t *testing.T) {214 type nErr struct {215 n int64216 err error217 }218 type justReader struct {219 io.Reader220 }221 type justWriter struct {222 io.Writer223 }224 discard := justWriter{ioutil.Discard} // hide ReadFrom225 var with, withOut nErr226 with.n, with.err = io.Copy(discard, NewReader(nil))227 withOut.n, withOut.err = io.Copy(discard, justReader{NewReader(nil)})228 if with != withOut {229 t.Errorf("behavior differs: with = %#v; without: %#v", with, withOut)230 }231}232// tests that Len is affected by reads, but Size is not.233func TestReaderLenSize(t *testing.T) {234 r := NewReader([]byte("abc"))235 io.CopyN(ioutil.Discard, r, 1)236 if r.Len() != 2 {237 t.Errorf("Len = %d; want 2", r.Len())238 }239 if r.Size() != 3 {240 t.Errorf("Size = %d; want 3", r.Size())241 }242}243func TestReaderReset(t *testing.T) {244 r := NewReader([]byte("ä¸ç"))245 if _, _, err := r.ReadRune(); err != nil {246 t.Errorf("ReadRune: unexpected error: %v", err)247 }248 const want = "abcdef"249 r.Reset([]byte(want))250 if err := r.UnreadRune(); err == nil {251 t.Errorf("UnreadRune: expected error, got nil")252 }253 buf, err := ioutil.ReadAll(r)254 if err != nil {255 t.Errorf("ReadAll: unexpected error: %v", err)256 }257 if got := string(buf); got != want {258 t.Errorf("ReadAll: got %q, want %q", got, want)259 }260}261func TestReaderZero(t *testing.T) {262 if l := (&Reader{}).Len(); l != 0 {263 t.Errorf("Len: got %d, want 0", l)264 }265 if n, err := (&Reader{}).Read(nil); n != 0 || err != io.EOF {266 t.Errorf("Read: got %d, %v; want 0, io.EOF", n, err)267 }268 if n, err := (&Reader{}).ReadAt(nil, 11); n != 0 || err != io.EOF {269 t.Errorf("ReadAt: got %d, %v; want 0, io.EOF", n, err)270 }271 if b, err := (&Reader{}).ReadByte(); b != 0 || err != io.EOF {272 t.Errorf("ReadByte: got %d, %v; want 0, io.EOF", b, err)273 }274 if ch, size, err := (&Reader{}).ReadRune(); ch != 0 || size != 0 || err != io.EOF {275 t.Errorf("ReadRune: got %d, %d, %v; want 0, 0, io.EOF", ch, size, err)276 }277 if offset, err := (&Reader{}).Seek(11, io.SeekStart); offset != 11 || err != nil {278 t.Errorf("Seek: got %d, %v; want 11, nil", offset, err)279 }280 if s := (&Reader{}).Size(); s != 0 {281 t.Errorf("Size: got %d, want 0", s)282 }283 if (&Reader{}).UnreadByte() == nil {284 t.Errorf("UnreadByte: got nil, want error")285 }286 if (&Reader{}).UnreadRune() == nil {287 t.Errorf("UnreadRune: got nil, want error")288 }289 if n, err := (&Reader{}).WriteTo(ioutil.Discard); n != 0 || err != nil {290 t.Errorf("WriteTo: got %d, %v; want 0, nil", n, err)291 }292}...
errors_test.go
Source:errors_test.go
1package errors2import (3 "errors"4 "fmt"5 "io"6 "reflect"7 "testing"8)9func TestNew(t *testing.T) {10 tests := []struct {11 err string12 want error13 }{14 {"", fmt.Errorf("")},15 {"foo", fmt.Errorf("foo")},16 {"foo", New("foo")},17 {"string with format specifiers: %v", errors.New("string with format specifiers: %v")},18 }19 for _, tt := range tests {20 got := New(tt.err)21 if got.Error() != tt.want.Error() {22 t.Errorf("New.Error(): got: %q, want %q", got, tt.want)23 }24 }25}26func TestWrapNil(t *testing.T) {27 got := Wrap(nil, "no error")28 if got != nil {29 t.Errorf("Wrap(nil, \"no error\"): got %#v, expected nil", got)30 }31}32func TestWrap(t *testing.T) {33 tests := []struct {34 err error35 message string36 want string37 }{38 {io.EOF, "read error", "read error: EOF"},39 {Wrap(io.EOF, "read error"), "client error", "client error: read error: EOF"},40 }41 for _, tt := range tests {42 got := Wrap(tt.err, tt.message).Error()43 if got != tt.want {44 t.Errorf("Wrap(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want)45 }46 }47}48type nilError struct{}49func (nilError) Error() string { return "nil error" }50func TestCause(t *testing.T) {51 x := New("error")52 tests := []struct {53 err error54 want error55 }{{56 // nil error is nil57 err: nil,58 want: nil,59 }, {60 // explicit nil error is nil61 err: (error)(nil),62 want: nil,63 }, {64 // typed nil is nil65 err: (*nilError)(nil),66 want: (*nilError)(nil),67 }, {68 // uncaused error is unaffected69 err: io.EOF,70 want: io.EOF,71 }, {72 // caused error returns cause73 err: Wrap(io.EOF, "ignored"),74 want: io.EOF,75 }, {76 err: x, // return from errors.New77 want: x,78 }, {79 WithMessage(nil, "whoops"),80 nil,81 }, {82 WithMessage(io.EOF, "whoops"),83 io.EOF,84 }, {85 WithStack(nil),86 nil,87 }, {88 WithStack(io.EOF),89 io.EOF,90 }}91 for i, tt := range tests {92 got := Cause(tt.err)93 if !reflect.DeepEqual(got, tt.want) {94 t.Errorf("test %d: got %#v, want %#v", i+1, got, tt.want)95 }96 }97}98func TestWrapfNil(t *testing.T) {99 got := Wrapf(nil, "no error")100 if got != nil {101 t.Errorf("Wrapf(nil, \"no error\"): got %#v, expected nil", got)102 }103}104func TestWrapf(t *testing.T) {105 tests := []struct {106 err error107 message string108 want string109 }{110 {io.EOF, "read error", "read error: EOF"},111 {Wrapf(io.EOF, "read error without format specifiers"), "client error", "client error: read error without format specifiers: EOF"},112 {Wrapf(io.EOF, "read error with %d format specifier", 1), "client error", "client error: read error with 1 format specifier: EOF"},113 }114 for _, tt := range tests {115 got := Wrapf(tt.err, tt.message).Error()116 if got != tt.want {117 t.Errorf("Wrapf(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want)118 }119 }120}121func TestErrorf(t *testing.T) {122 tests := []struct {123 err error124 want string125 }{126 {Errorf("read error without format specifiers"), "read error without format specifiers"},127 {Errorf("read error with %d format specifier", 1), "read error with 1 format specifier"},128 }129 for _, tt := range tests {130 got := tt.err.Error()131 if got != tt.want {132 t.Errorf("Errorf(%v): got: %q, want %q", tt.err, got, tt.want)133 }134 }135}136func TestWithStackNil(t *testing.T) {137 got := WithStack(nil)138 if got != nil {139 t.Errorf("WithStack(nil): got %#v, expected nil", got)140 }141}142func TestWithStack(t *testing.T) {143 tests := []struct {144 err error145 want string146 }{147 {io.EOF, "EOF"},148 {WithStack(io.EOF), "EOF"},149 }150 for _, tt := range tests {151 got := WithStack(tt.err).Error()152 if got != tt.want {153 t.Errorf("WithStack(%v): got: %v, want %v", tt.err, got, tt.want)154 }155 }156}157func TestWithMessageNil(t *testing.T) {158 got := WithMessage(nil, "no error")159 if got != nil {160 t.Errorf("WithMessage(nil, \"no error\"): got %#v, expected nil", got)161 }162}163func TestWithMessage(t *testing.T) {164 tests := []struct {165 err error166 message string167 want string168 }{169 {io.EOF, "read error", "read error: EOF"},170 {WithMessage(io.EOF, "read error"), "client error", "client error: read error: EOF"},171 }172 for _, tt := range tests {173 got := WithMessage(tt.err, tt.message).Error()174 if got != tt.want {175 t.Errorf("WithMessage(%v, %q): got: %q, want %q", tt.err, tt.message, got, tt.want)176 }177 }178}179func TestWithMessagefNil(t *testing.T) {180 got := WithMessagef(nil, "no error")181 if got != nil {182 t.Errorf("WithMessage(nil, \"no error\"): got %#v, expected nil", got)183 }184}185func TestWithMessagef(t *testing.T) {186 tests := []struct {187 err error188 message string189 want string190 }{191 {io.EOF, "read error", "read error: EOF"},192 {WithMessagef(io.EOF, "read error without format specifier"), "client error", "client error: read error without format specifier: EOF"},193 {WithMessagef(io.EOF, "read error with %d format specifier", 1), "client error", "client error: read error with 1 format specifier: EOF"},194 }195 for _, tt := range tests {196 got := WithMessagef(tt.err, tt.message).Error()197 if got != tt.want {198 t.Errorf("WithMessage(%v, %q): got: %q, want %q", tt.err, tt.message, got, tt.want)199 }200 }201}202// errors.New, etc values are not expected to be compared by value203// but the change in errors#27 made them incomparable. Assert that204// various kinds of errors have a functional equality operator, even205// if the result of that equality is always false.206func TestErrorEquality(t *testing.T) {207 vals := []error{208 nil,209 io.EOF,210 errors.New("EOF"),211 New("EOF"),212 Errorf("EOF"),213 Wrap(io.EOF, "EOF"),214 Wrapf(io.EOF, "EOF%d", 2),215 WithMessage(nil, "whoops"),216 WithMessage(io.EOF, "whoops"),217 WithStack(io.EOF),218 WithStack(nil),219 }220 for i := range vals {221 for j := range vals {222 _ = vals[i] == vals[j] // mustn't panic223 }224 }225}...
Read
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4 f, err := os.Open("test.txt")5 if err != nil {6 fmt.Println(err)7 }8 defer f.Close()9 buf := make([]byte, 1024)10 n, err := f.Read(buf)11 if err != nil {12 fmt.Println(err)13 }14 fmt.Println(string(buf[:n]))15}16import (17func main() {18 fmt.Println("Hello, playground")19 f, err := os.Open("test.txt")20 if err != nil {21 fmt.Println(err)22 }23 defer f.Close()24 buf := make([]byte, 1024)25 n, err := f.ReadAt(buf, 0)26 if err != nil {27 fmt.Println(err)28 }29 fmt.Println(string(buf[:n]))30}31import (32func main() {33 fmt.Println("Hello, playground")34 f, err := os.OpenFile("test.txt", os.O_RDWR, 0)35 if err != nil {36 fmt.Println(err)37 }38 defer f.Close()39 n, err := f.Write([]byte("Hello"))40 if err != nil {41 fmt.Println(err)42 }43 fmt.Println(n)44}45import (46func main() {47 fmt.Println("Hello, playground")48 f, err := os.OpenFile("test.txt", os.O_RDWR, 0)49 if err != nil {50 fmt.Println(err)51 }52 defer f.Close()53 n, err := f.WriteAt([]byte("Hello"), 0)54 if err != nil {55 fmt.Println(err)56 }57 fmt.Println(n)58}59import (60func main() {61 fmt.Println("Hello, playground")62 f, err := os.OpenFile("test.txt", os.O_RDWR, 0)63 if err != nil {64 fmt.Println(err)
Read
Using AI Code Generation
1func main() {2 fmt.Println("Hello World")3 got.Read()4}5func main() {6 fmt.Println("Hello World")7 got.Write()8}9func main() {10 fmt.Println("Hello World")11 got.Read()12}13func main() {14 fmt.Println("Hello World")15 got.Write()16}17func main() {18 fmt.Println("Hello World")19 got.Read()20}21func main() {22 fmt.Println("Hello World")23 got.Write()24}25func main() {26 fmt.Println("Hello World")27 got.Read()28}29func main() {30 fmt.Println("Hello World")31 got.Write()32}33func main() {34 fmt.Println("Hello World")35 got.Read()36}37func main() {38 fmt.Println("Hello World")39 got.Write()40}41func main() {42 fmt.Println("Hello World")43 got.Read()44}45func main() {46 fmt.Println("Hello World")47 got.Write()48}49func main() {50 fmt.Println("Hello World")51 got.Read()52}53func main() {
Read
Using AI Code Generation
1import (2func main() {3 fmt.Println(got.Read())4}5import (6func Read() string {7 file, err := ioutil.ReadFile("2.go")8 if err != nil {9 fmt.Println("error")10 }11 return string(file)12}
Read
Using AI Code Generation
1func main() {2 got := got.NewGot()3 got.Read()4}5func main() {6 got := got.NewGot()7 got.Write()8}9func main() {10 got := got.NewGot()11 got.Read()12}13func main() {14 got := got.NewGot()15 got.Write()16}17func main() {18 got := got.NewGot()19 got.Read()20}21func main() {22 got := got.NewGot()23 got.Write()24}25func main() {26 got := got.NewGot()27 got.Read()28}29func main() {30 got := got.NewGot()31 got.Write()32}33func main() {34 got := got.NewGot()35 got.Read()36}37func main() {38 got := got.NewGot()39 got.Write()40}41func main() {
Read
Using AI Code Generation
1import "fmt"2func main(){3 fmt.Println("Enter an integer:")4 fmt.Scanln(&a)5 r := got.Read(a)6 fmt.Println("The integer is:", r)7}8func Read(a int) int{9}10import "fmt"11func main() {12 fmt.Println("Hello, World!")13}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!