Nigel Tao | fb0b172 | 2019-08-06 16:15:09 +1000 | [diff] [blame] | 1 | // Copyright 2019 The Wuffs Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Nigel Tao | 788479d | 2021-08-22 10:52:51 +1000 | [diff] [blame] | 15 | //go:build ignore |
Nigel Tao | fb0b172 | 2019-08-06 16:15:09 +1000 | [diff] [blame] | 16 | // +build ignore |
| 17 | |
| 18 | package main |
| 19 | |
| 20 | // rac-random-seek-test.go compresses stdin to the RAC file format, then checks |
| 21 | // that repeatedly randomly accessing that RAC file recovers the original input. |
| 22 | // |
| 23 | // Usage: go run rac-random-seek-test.go < input |
| 24 | |
| 25 | import ( |
| 26 | "bytes" |
| 27 | "fmt" |
| 28 | "io" |
Nigel Tao | fb0b172 | 2019-08-06 16:15:09 +1000 | [diff] [blame] | 29 | "math/rand" |
| 30 | "os" |
| 31 | |
| 32 | "github.com/google/wuffs/lib/rac" |
| 33 | "github.com/google/wuffs/lib/raczlib" |
| 34 | ) |
| 35 | |
| 36 | func main() { |
| 37 | if err := main1(); err != nil { |
| 38 | os.Stderr.WriteString(err.Error() + "\n") |
| 39 | os.Exit(1) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func main1() error { |
Nigel Tao | 226c476 | 2021-08-22 11:05:43 +1000 | [diff] [blame] | 44 | input, err := io.ReadAll(os.Stdin) |
Nigel Tao | fb0b172 | 2019-08-06 16:15:09 +1000 | [diff] [blame] | 45 | if err != nil { |
| 46 | return err |
| 47 | } |
| 48 | buf := &bytes.Buffer{} |
| 49 | const dChunkSize = 256 |
Nigel Tao | f53bee3 | 2019-08-24 07:56:53 +1000 | [diff] [blame] | 50 | w := &rac.Writer{ |
| 51 | Writer: buf, |
| 52 | CodecWriter: &raczlib.CodecWriter{}, |
| 53 | DChunkSize: dChunkSize, |
Nigel Tao | fb0b172 | 2019-08-06 16:15:09 +1000 | [diff] [blame] | 54 | } |
| 55 | if _, err := w.Write(input); err != nil { |
| 56 | return err |
| 57 | } |
| 58 | if err := w.Close(); err != nil { |
| 59 | return err |
| 60 | } |
| 61 | compressed := buf.Bytes() |
Nigel Tao | f53bee3 | 2019-08-24 07:56:53 +1000 | [diff] [blame] | 62 | r := &rac.Reader{ |
Nigel Tao | ca4a6c8 | 2019-08-24 09:34:12 +1000 | [diff] [blame] | 63 | ReadSeeker: bytes.NewReader(compressed), |
| 64 | CompressedSize: int64(buf.Len()), |
| 65 | CodecReaders: []rac.CodecReader{&raczlib.CodecReader{}}, |
Nigel Tao | fb0b172 | 2019-08-06 16:15:09 +1000 | [diff] [blame] | 66 | } |
| 67 | numChunks, err := countRACChunks(compressed) |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | if want := (len(input) + dChunkSize - 1) / dChunkSize; numChunks != want { |
| 72 | return fmt.Errorf("numChunks: got %d, want %d", numChunks, want) |
| 73 | } |
| 74 | |
| 75 | fmt.Printf("%8d uncompressed bytes\n", len(input)) |
| 76 | fmt.Printf("%8d compressed bytes\n", len(compressed)) |
| 77 | fmt.Printf("%8d RAC chunks\n", numChunks) |
| 78 | |
| 79 | got := make([]byte, len(input)) |
| 80 | if _, err := io.ReadFull(r, got); err != nil { |
| 81 | return fmt.Errorf("ReadFull: %v", err) |
| 82 | } else if !bytes.Equal(got, input) { |
| 83 | return fmt.Errorf("ReadFull: mismatch") |
| 84 | } |
| 85 | |
| 86 | for i, rng := uint64(0), rand.New(rand.NewSource(1)); ; i++ { |
| 87 | m := rng.Intn(len(input) + 1) |
| 88 | n := rng.Intn(len(input) + 1) |
| 89 | if m > n { |
| 90 | m, n = n, m |
| 91 | } |
| 92 | |
| 93 | got = got[:n-m] |
| 94 | if _, err := r.Seek(int64(m), io.SeekStart); err != nil { |
| 95 | return fmt.Errorf("i=%d, m=%d, n=%d: Seek: %v", i, m, n, err) |
| 96 | } |
| 97 | if _, err := io.ReadFull(r, got); err != nil { |
| 98 | return fmt.Errorf("i=%d, m=%d, n=%d: ReadFull: %v", i, m, n, err) |
| 99 | } |
| 100 | |
| 101 | want := input[m:n] |
| 102 | if !bytes.Equal(got, want) { |
| 103 | return fmt.Errorf("i=%d, m=%d, n=%d: mismatch", i, m, n) |
| 104 | } |
| 105 | |
| 106 | if i%1024 == 0 { |
| 107 | fmt.Printf("i=%d: ok\n", i) |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func countRACChunks(compressed []byte) (int, error) { |
Nigel Tao | f53bee3 | 2019-08-24 07:56:53 +1000 | [diff] [blame] | 113 | r := &rac.ChunkReader{ |
Nigel Tao | ca4a6c8 | 2019-08-24 09:34:12 +1000 | [diff] [blame] | 114 | ReadSeeker: bytes.NewReader(compressed), |
| 115 | CompressedSize: int64(len(compressed)), |
Nigel Tao | fb0b172 | 2019-08-06 16:15:09 +1000 | [diff] [blame] | 116 | } |
| 117 | for n := 0; ; n++ { |
Nigel Tao | f53bee3 | 2019-08-24 07:56:53 +1000 | [diff] [blame] | 118 | if _, err := r.NextChunk(); err == io.EOF { |
Nigel Tao | fb0b172 | 2019-08-06 16:15:09 +1000 | [diff] [blame] | 119 | return n, nil |
| 120 | } else if err != nil { |
| 121 | return 0, err |
| 122 | } |
| 123 | } |
| 124 | } |