snappy_test.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. // Copyright 2011 The Snappy-Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package snappy
  5. import (
  6. "bytes"
  7. "encoding/binary"
  8. "flag"
  9. "fmt"
  10. "io"
  11. "io/ioutil"
  12. "math/rand"
  13. "net/http"
  14. "os"
  15. "os/exec"
  16. "path/filepath"
  17. "runtime"
  18. "strings"
  19. "testing"
  20. )
  21. var download = flag.Bool("download", false, "If true, download any missing files before running benchmarks")
  22. // goEncoderShouldMatchCppEncoder is whether to test that the algorithm used by
  23. // Go's encoder matches byte-for-byte what the C++ snappy encoder produces, on
  24. // this GOARCH. There is more than one valid encoding of any given input, and
  25. // there is more than one good algorithm along the frontier of trading off
  26. // throughput for output size. Nonetheless, we presume that the C++ encoder's
  27. // algorithm is a good one and has been tested on a wide range of inputs, so
  28. // matching that exactly should mean that the Go encoder's algorithm is also
  29. // good, without needing to gather our own corpus of test data.
  30. //
  31. // The exact algorithm used by the C++ code is potentially endian dependent, as
  32. // it puns a byte pointer to a uint32 pointer to load, hash and compare 4 bytes
  33. // at a time. The Go implementation is endian agnostic, in that its output is
  34. // the same (as little-endian C++ code), regardless of the CPU's endianness.
  35. //
  36. // Thus, when comparing Go's output to C++ output generated beforehand, such as
  37. // the "testdata/pi.txt.rawsnappy" file generated by C++ code on a little-
  38. // endian system, we can run that test regardless of the runtime.GOARCH value.
  39. //
  40. // When comparing Go's output to dynamically generated C++ output, i.e. the
  41. // result of fork/exec'ing a C++ program, we can run that test only on
  42. // little-endian systems, because the C++ output might be different on
  43. // big-endian systems. The runtime package doesn't export endianness per se,
  44. // but we can restrict this match-C++ test to common little-endian systems.
  45. const goEncoderShouldMatchCppEncoder = runtime.GOARCH == "386" || runtime.GOARCH == "amd64" || runtime.GOARCH == "arm"
  46. func TestMaxEncodedLenOfMaxBlockSize(t *testing.T) {
  47. got := maxEncodedLenOfMaxBlockSize
  48. want := MaxEncodedLen(maxBlockSize)
  49. if got != want {
  50. t.Fatalf("got %d, want %d", got, want)
  51. }
  52. }
  53. func cmp(a, b []byte) error {
  54. if bytes.Equal(a, b) {
  55. return nil
  56. }
  57. if len(a) != len(b) {
  58. return fmt.Errorf("got %d bytes, want %d", len(a), len(b))
  59. }
  60. for i := range a {
  61. if a[i] != b[i] {
  62. return fmt.Errorf("byte #%d: got 0x%02x, want 0x%02x", i, a[i], b[i])
  63. }
  64. }
  65. return nil
  66. }
  67. func roundtrip(b, ebuf, dbuf []byte) error {
  68. d, err := Decode(dbuf, Encode(ebuf, b))
  69. if err != nil {
  70. return fmt.Errorf("decoding error: %v", err)
  71. }
  72. if err := cmp(d, b); err != nil {
  73. return fmt.Errorf("roundtrip mismatch: %v", err)
  74. }
  75. return nil
  76. }
  77. func TestEmpty(t *testing.T) {
  78. if err := roundtrip(nil, nil, nil); err != nil {
  79. t.Fatal(err)
  80. }
  81. }
  82. func TestSmallCopy(t *testing.T) {
  83. for _, ebuf := range [][]byte{nil, make([]byte, 20), make([]byte, 64)} {
  84. for _, dbuf := range [][]byte{nil, make([]byte, 20), make([]byte, 64)} {
  85. for i := 0; i < 32; i++ {
  86. s := "aaaa" + strings.Repeat("b", i) + "aaaabbbb"
  87. if err := roundtrip([]byte(s), ebuf, dbuf); err != nil {
  88. t.Errorf("len(ebuf)=%d, len(dbuf)=%d, i=%d: %v", len(ebuf), len(dbuf), i, err)
  89. }
  90. }
  91. }
  92. }
  93. }
  94. func TestSmallRand(t *testing.T) {
  95. rng := rand.New(rand.NewSource(1))
  96. for n := 1; n < 20000; n += 23 {
  97. b := make([]byte, n)
  98. for i := range b {
  99. b[i] = uint8(rng.Intn(256))
  100. }
  101. if err := roundtrip(b, nil, nil); err != nil {
  102. t.Fatal(err)
  103. }
  104. }
  105. }
  106. func TestSmallRegular(t *testing.T) {
  107. for n := 1; n < 20000; n += 23 {
  108. b := make([]byte, n)
  109. for i := range b {
  110. b[i] = uint8(i%10 + 'a')
  111. }
  112. if err := roundtrip(b, nil, nil); err != nil {
  113. t.Fatal(err)
  114. }
  115. }
  116. }
  117. func TestInvalidVarint(t *testing.T) {
  118. testCases := []struct {
  119. desc string
  120. input string
  121. }{{
  122. "invalid varint, final byte has continuation bit set",
  123. "\xff",
  124. }, {
  125. "invalid varint, value overflows uint64",
  126. "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00",
  127. }, {
  128. // https://github.com/google/snappy/blob/master/format_description.txt
  129. // says that "the stream starts with the uncompressed length [as a
  130. // varint] (up to a maximum of 2^32 - 1)".
  131. "valid varint (as uint64), but value overflows uint32",
  132. "\x80\x80\x80\x80\x10",
  133. }}
  134. for _, tc := range testCases {
  135. input := []byte(tc.input)
  136. if _, err := DecodedLen(input); err != ErrCorrupt {
  137. t.Errorf("%s: DecodedLen: got %v, want ErrCorrupt", tc.desc, err)
  138. }
  139. if _, err := Decode(nil, input); err != ErrCorrupt {
  140. t.Errorf("%s: Decode: got %v, want ErrCorrupt", tc.desc, err)
  141. }
  142. }
  143. }
  144. func TestDecode(t *testing.T) {
  145. lit40Bytes := make([]byte, 40)
  146. for i := range lit40Bytes {
  147. lit40Bytes[i] = byte(i)
  148. }
  149. lit40 := string(lit40Bytes)
  150. testCases := []struct {
  151. desc string
  152. input string
  153. want string
  154. wantErr error
  155. }{{
  156. `decodedLen=0; valid input`,
  157. "\x00",
  158. "",
  159. nil,
  160. }, {
  161. `decodedLen=3; tagLiteral, 0-byte length; length=3; valid input`,
  162. "\x03" + "\x08\xff\xff\xff",
  163. "\xff\xff\xff",
  164. nil,
  165. }, {
  166. `decodedLen=2; tagLiteral, 0-byte length; length=3; not enough dst bytes`,
  167. "\x02" + "\x08\xff\xff\xff",
  168. "",
  169. ErrCorrupt,
  170. }, {
  171. `decodedLen=3; tagLiteral, 0-byte length; length=3; not enough src bytes`,
  172. "\x03" + "\x08\xff\xff",
  173. "",
  174. ErrCorrupt,
  175. }, {
  176. `decodedLen=40; tagLiteral, 0-byte length; length=40; valid input`,
  177. "\x28" + "\x9c" + lit40,
  178. lit40,
  179. nil,
  180. }, {
  181. `decodedLen=1; tagLiteral, 1-byte length; not enough length bytes`,
  182. "\x01" + "\xf0",
  183. "",
  184. ErrCorrupt,
  185. }, {
  186. `decodedLen=3; tagLiteral, 1-byte length; length=3; valid input`,
  187. "\x03" + "\xf0\x02\xff\xff\xff",
  188. "\xff\xff\xff",
  189. nil,
  190. }, {
  191. `decodedLen=1; tagLiteral, 2-byte length; not enough length bytes`,
  192. "\x01" + "\xf4\x00",
  193. "",
  194. ErrCorrupt,
  195. }, {
  196. `decodedLen=3; tagLiteral, 2-byte length; length=3; valid input`,
  197. "\x03" + "\xf4\x02\x00\xff\xff\xff",
  198. "\xff\xff\xff",
  199. nil,
  200. }, {
  201. `decodedLen=1; tagLiteral, 3-byte length; not enough length bytes`,
  202. "\x01" + "\xf8\x00\x00",
  203. "",
  204. ErrCorrupt,
  205. }, {
  206. `decodedLen=3; tagLiteral, 3-byte length; length=3; valid input`,
  207. "\x03" + "\xf8\x02\x00\x00\xff\xff\xff",
  208. "\xff\xff\xff",
  209. nil,
  210. }, {
  211. `decodedLen=1; tagLiteral, 4-byte length; not enough length bytes`,
  212. "\x01" + "\xfc\x00\x00\x00",
  213. "",
  214. ErrCorrupt,
  215. }, {
  216. `decodedLen=1; tagLiteral, 4-byte length; length=3; not enough dst bytes`,
  217. "\x01" + "\xfc\x02\x00\x00\x00\xff\xff\xff",
  218. "",
  219. ErrCorrupt,
  220. }, {
  221. `decodedLen=4; tagLiteral, 4-byte length; length=3; not enough src bytes`,
  222. "\x04" + "\xfc\x02\x00\x00\x00\xff",
  223. "",
  224. ErrCorrupt,
  225. }, {
  226. `decodedLen=3; tagLiteral, 4-byte length; length=3; valid input`,
  227. "\x03" + "\xfc\x02\x00\x00\x00\xff\xff\xff",
  228. "\xff\xff\xff",
  229. nil,
  230. }, {
  231. `decodedLen=4; tagCopy1, 1 extra length|offset byte; not enough extra bytes`,
  232. "\x04" + "\x01",
  233. "",
  234. ErrCorrupt,
  235. }, {
  236. `decodedLen=4; tagCopy2, 2 extra length|offset bytes; not enough extra bytes`,
  237. "\x04" + "\x02\x00",
  238. "",
  239. ErrCorrupt,
  240. }, {
  241. `decodedLen=4; tagCopy4; unsupported COPY_4 tag`,
  242. "\x04" + "\x03\x00\x00\x00\x00",
  243. "",
  244. errUnsupportedCopy4Tag,
  245. }, {
  246. `decodedLen=4; tagLiteral (4 bytes "abcd"); valid input`,
  247. "\x04" + "\x0cabcd",
  248. "abcd",
  249. nil,
  250. }, {
  251. `decodedLen=13; tagLiteral (4 bytes "abcd"); tagCopy1; length=9 offset=4; valid input`,
  252. "\x0d" + "\x0cabcd" + "\x15\x04",
  253. "abcdabcdabcda",
  254. nil,
  255. }, {
  256. `decodedLen=8; tagLiteral (4 bytes "abcd"); tagCopy1; length=4 offset=4; valid input`,
  257. "\x08" + "\x0cabcd" + "\x01\x04",
  258. "abcdabcd",
  259. nil,
  260. }, {
  261. `decodedLen=8; tagLiteral (4 bytes "abcd"); tagCopy1; length=4 offset=2; valid input`,
  262. "\x08" + "\x0cabcd" + "\x01\x02",
  263. "abcdcdcd",
  264. nil,
  265. }, {
  266. `decodedLen=8; tagLiteral (4 bytes "abcd"); tagCopy1; length=4 offset=1; valid input`,
  267. "\x08" + "\x0cabcd" + "\x01\x01",
  268. "abcddddd",
  269. nil,
  270. }, {
  271. `decodedLen=8; tagLiteral (4 bytes "abcd"); tagCopy1; length=4 offset=0; zero offset`,
  272. "\x08" + "\x0cabcd" + "\x01\x00",
  273. "",
  274. ErrCorrupt,
  275. }, {
  276. `decodedLen=9; tagLiteral (4 bytes "abcd"); tagCopy1; length=4 offset=4; inconsistent dLen`,
  277. "\x09" + "\x0cabcd" + "\x01\x04",
  278. "",
  279. ErrCorrupt,
  280. }, {
  281. `decodedLen=8; tagLiteral (4 bytes "abcd"); tagCopy1; length=4 offset=5; offset too large`,
  282. "\x08" + "\x0cabcd" + "\x01\x05",
  283. "",
  284. ErrCorrupt,
  285. }, {
  286. `decodedLen=7; tagLiteral (4 bytes "abcd"); tagCopy1; length=4 offset=4; length too large`,
  287. "\x07" + "\x0cabcd" + "\x01\x04",
  288. "",
  289. ErrCorrupt,
  290. }, {
  291. `decodedLen=6; tagLiteral (4 bytes "abcd"); tagCopy2; length=2 offset=3; valid input`,
  292. "\x06" + "\x0cabcd" + "\x06\x03\x00",
  293. "abcdbc",
  294. nil,
  295. }}
  296. const (
  297. // notPresentXxx defines a range of byte values [0xa0, 0xc5) that are
  298. // not present in either the input or the output. It is written to dBuf
  299. // to check that Decode does not write bytes past the end of
  300. // dBuf[:dLen].
  301. //
  302. // The magic number 37 was chosen because it is prime. A more 'natural'
  303. // number like 32 might lead to a false negative if, for example, a
  304. // byte was incorrectly copied 4*8 bytes later.
  305. notPresentBase = 0xa0
  306. notPresentLen = 37
  307. )
  308. var dBuf [100]byte
  309. loop:
  310. for i, tc := range testCases {
  311. input := []byte(tc.input)
  312. for _, x := range input {
  313. if notPresentBase <= x && x < notPresentBase+notPresentLen {
  314. t.Errorf("#%d (%s): input shouldn't contain %#02x\ninput: % x", i, tc.desc, x, input)
  315. continue loop
  316. }
  317. }
  318. dLen, n := binary.Uvarint(input)
  319. if n <= 0 {
  320. t.Errorf("#%d (%s): invalid varint-encoded dLen", i, tc.desc)
  321. continue
  322. }
  323. if dLen > uint64(len(dBuf)) {
  324. t.Errorf("#%d (%s): dLen %d is too large", i, tc.desc, dLen)
  325. continue
  326. }
  327. for j := range dBuf {
  328. dBuf[j] = byte(notPresentBase + j%notPresentLen)
  329. }
  330. g, gotErr := Decode(dBuf[:], input)
  331. if got := string(g); got != tc.want || gotErr != tc.wantErr {
  332. t.Errorf("#%d (%s):\ngot %q, %v\nwant %q, %v",
  333. i, tc.desc, got, gotErr, tc.want, tc.wantErr)
  334. continue
  335. }
  336. for j, x := range dBuf {
  337. if uint64(j) < dLen {
  338. continue
  339. }
  340. if w := byte(notPresentBase + j%notPresentLen); x != w {
  341. t.Errorf("#%d (%s): Decode overrun: dBuf[%d] was modified: got %#02x, want %#02x\ndBuf: % x",
  342. i, tc.desc, j, x, w, dBuf)
  343. continue loop
  344. }
  345. }
  346. }
  347. }
  348. // TestDecodeLengthOffset tests decoding an encoding of the form literal +
  349. // copy-length-offset + literal. For example: "abcdefghijkl" + "efghij" + "AB".
  350. func TestDecodeLengthOffset(t *testing.T) {
  351. const (
  352. prefix = "abcdefghijklmnopqr"
  353. suffix = "ABCDEFGHIJKLMNOPQR"
  354. // notPresentXxx defines a range of byte values [0xa0, 0xc5) that are
  355. // not present in either the input or the output. It is written to
  356. // gotBuf to check that Decode does not write bytes past the end of
  357. // gotBuf[:totalLen].
  358. //
  359. // The magic number 37 was chosen because it is prime. A more 'natural'
  360. // number like 32 might lead to a false negative if, for example, a
  361. // byte was incorrectly copied 4*8 bytes later.
  362. notPresentBase = 0xa0
  363. notPresentLen = 37
  364. )
  365. var gotBuf, wantBuf, inputBuf [128]byte
  366. for length := 1; length <= 18; length++ {
  367. for offset := 1; offset <= 18; offset++ {
  368. loop:
  369. for suffixLen := 0; suffixLen <= 18; suffixLen++ {
  370. totalLen := len(prefix) + length + suffixLen
  371. inputLen := binary.PutUvarint(inputBuf[:], uint64(totalLen))
  372. inputBuf[inputLen] = tagLiteral + 4*byte(len(prefix)-1)
  373. inputLen++
  374. inputLen += copy(inputBuf[inputLen:], prefix)
  375. inputBuf[inputLen+0] = tagCopy2 + 4*byte(length-1)
  376. inputBuf[inputLen+1] = byte(offset)
  377. inputBuf[inputLen+2] = 0x00
  378. inputLen += 3
  379. if suffixLen > 0 {
  380. inputBuf[inputLen] = tagLiteral + 4*byte(suffixLen-1)
  381. inputLen++
  382. inputLen += copy(inputBuf[inputLen:], suffix[:suffixLen])
  383. }
  384. input := inputBuf[:inputLen]
  385. for i := range gotBuf {
  386. gotBuf[i] = byte(notPresentBase + i%notPresentLen)
  387. }
  388. got, err := Decode(gotBuf[:], input)
  389. if err != nil {
  390. t.Errorf("length=%d, offset=%d; suffixLen=%d: %v", length, offset, suffixLen, err)
  391. continue
  392. }
  393. wantLen := 0
  394. wantLen += copy(wantBuf[wantLen:], prefix)
  395. for i := 0; i < length; i++ {
  396. wantBuf[wantLen] = wantBuf[wantLen-offset]
  397. wantLen++
  398. }
  399. wantLen += copy(wantBuf[wantLen:], suffix[:suffixLen])
  400. want := wantBuf[:wantLen]
  401. for _, x := range input {
  402. if notPresentBase <= x && x < notPresentBase+notPresentLen {
  403. t.Errorf("length=%d, offset=%d; suffixLen=%d: input shouldn't contain %#02x\ninput: % x",
  404. length, offset, suffixLen, x, input)
  405. continue loop
  406. }
  407. }
  408. for i, x := range gotBuf {
  409. if i < totalLen {
  410. continue
  411. }
  412. if w := byte(notPresentBase + i%notPresentLen); x != w {
  413. t.Errorf("length=%d, offset=%d; suffixLen=%d; totalLen=%d: "+
  414. "Decode overrun: gotBuf[%d] was modified: got %#02x, want %#02x\ngotBuf: % x",
  415. length, offset, suffixLen, totalLen, i, x, w, gotBuf)
  416. continue loop
  417. }
  418. }
  419. for _, x := range want {
  420. if notPresentBase <= x && x < notPresentBase+notPresentLen {
  421. t.Errorf("length=%d, offset=%d; suffixLen=%d: want shouldn't contain %#02x\nwant: % x",
  422. length, offset, suffixLen, x, want)
  423. continue loop
  424. }
  425. }
  426. if !bytes.Equal(got, want) {
  427. t.Errorf("length=%d, offset=%d; suffixLen=%d:\ninput % x\ngot % x\nwant % x",
  428. length, offset, suffixLen, input, got, want)
  429. continue
  430. }
  431. }
  432. }
  433. }
  434. }
  435. func TestDecodeGoldenInput(t *testing.T) {
  436. src, err := ioutil.ReadFile("testdata/pi.txt.rawsnappy")
  437. if err != nil {
  438. t.Fatalf("ReadFile: %v", err)
  439. }
  440. got, err := Decode(nil, src)
  441. if err != nil {
  442. t.Fatalf("Decode: %v", err)
  443. }
  444. want, err := ioutil.ReadFile("testdata/pi.txt")
  445. if err != nil {
  446. t.Fatalf("ReadFile: %v", err)
  447. }
  448. if err := cmp(got, want); err != nil {
  449. t.Fatal(err)
  450. }
  451. }
  452. func TestEncodeGoldenInput(t *testing.T) {
  453. src, err := ioutil.ReadFile("testdata/pi.txt")
  454. if err != nil {
  455. t.Fatalf("ReadFile: %v", err)
  456. }
  457. got := Encode(nil, src)
  458. want, err := ioutil.ReadFile("testdata/pi.txt.rawsnappy")
  459. if err != nil {
  460. t.Fatalf("ReadFile: %v", err)
  461. }
  462. if err := cmp(got, want); err != nil {
  463. t.Fatal(err)
  464. }
  465. }
  466. const snappytoolCmdName = "cmd/snappytool/snappytool"
  467. func skipTestSameEncodingAsCpp() (msg string) {
  468. if !goEncoderShouldMatchCppEncoder {
  469. return fmt.Sprintf("skipping testing that the encoding is byte-for-byte identical to C++: GOARCH=%s", runtime.GOARCH)
  470. }
  471. if _, err := os.Stat(snappytoolCmdName); err != nil {
  472. return fmt.Sprintf("could not find snappytool: %v", err)
  473. }
  474. return ""
  475. }
  476. func runTestSameEncodingAsCpp(src []byte) error {
  477. got := Encode(nil, src)
  478. cmd := exec.Command(snappytoolCmdName, "-e")
  479. cmd.Stdin = bytes.NewReader(src)
  480. want, err := cmd.Output()
  481. if err != nil {
  482. return fmt.Errorf("could not run snappytool: %v", err)
  483. }
  484. return cmp(got, want)
  485. }
  486. func TestSameEncodingAsCppShortCopies(t *testing.T) {
  487. if msg := skipTestSameEncodingAsCpp(); msg != "" {
  488. t.Skip(msg)
  489. }
  490. src := bytes.Repeat([]byte{'a'}, 20)
  491. for i := 0; i <= len(src); i++ {
  492. if err := runTestSameEncodingAsCpp(src[:i]); err != nil {
  493. t.Errorf("i=%d: %v", i, err)
  494. }
  495. }
  496. }
  497. func TestSameEncodingAsCppLongFiles(t *testing.T) {
  498. if msg := skipTestSameEncodingAsCpp(); msg != "" {
  499. t.Skip(msg)
  500. }
  501. for i, tf := range testFiles {
  502. if err := downloadBenchmarkFiles(t, tf.filename); err != nil {
  503. t.Fatalf("failed to download testdata: %s", err)
  504. }
  505. data := readFile(t, filepath.Join(benchDir, tf.filename))
  506. if n := tf.sizeLimit; 0 < n && n < len(data) {
  507. data = data[:n]
  508. }
  509. if err := runTestSameEncodingAsCpp(data); err != nil {
  510. t.Errorf("i=%d: %v", i, err)
  511. }
  512. }
  513. }
  514. // TestSlowForwardCopyOverrun tests the "expand the pattern" algorithm
  515. // described in decode_amd64.s and its claim of a 10 byte overrun worst case.
  516. func TestSlowForwardCopyOverrun(t *testing.T) {
  517. const base = 100
  518. for length := 1; length < 18; length++ {
  519. for offset := 1; offset < 18; offset++ {
  520. highWaterMark := base
  521. d := base
  522. l := length
  523. o := offset
  524. // makeOffsetAtLeast8
  525. for o < 8 {
  526. if end := d + 8; highWaterMark < end {
  527. highWaterMark = end
  528. }
  529. l -= o
  530. d += o
  531. o += o
  532. }
  533. // fixUpSlowForwardCopy
  534. a := d
  535. d += l
  536. // finishSlowForwardCopy
  537. for l > 0 {
  538. if end := a + 8; highWaterMark < end {
  539. highWaterMark = end
  540. }
  541. a += 8
  542. l -= 8
  543. }
  544. dWant := base + length
  545. overrun := highWaterMark - dWant
  546. if d != dWant || overrun < 0 || 10 < overrun {
  547. t.Errorf("length=%d, offset=%d: d and overrun: got (%d, %d), want (%d, something in [0, 10])",
  548. length, offset, d, overrun, dWant)
  549. }
  550. }
  551. }
  552. }
  553. // TestEncodeNoiseThenRepeats encodes input for which the first half is very
  554. // incompressible and the second half is very compressible. The encoded form's
  555. // length should be closer to 50% of the original length than 100%.
  556. func TestEncodeNoiseThenRepeats(t *testing.T) {
  557. for _, origLen := range []int{32 * 1024, 256 * 1024, 2048 * 1024} {
  558. src := make([]byte, origLen)
  559. rng := rand.New(rand.NewSource(1))
  560. firstHalf, secondHalf := src[:origLen/2], src[origLen/2:]
  561. for i := range firstHalf {
  562. firstHalf[i] = uint8(rng.Intn(256))
  563. }
  564. for i := range secondHalf {
  565. secondHalf[i] = uint8(i >> 8)
  566. }
  567. dst := Encode(nil, src)
  568. if got, want := len(dst), origLen*3/4; got >= want {
  569. t.Errorf("origLen=%d: got %d encoded bytes, want less than %d", origLen, got, want)
  570. }
  571. }
  572. }
  573. func TestFramingFormat(t *testing.T) {
  574. // src is comprised of alternating 1e5-sized sequences of random
  575. // (incompressible) bytes and repeated (compressible) bytes. 1e5 was chosen
  576. // because it is larger than maxBlockSize (64k).
  577. src := make([]byte, 1e6)
  578. rng := rand.New(rand.NewSource(1))
  579. for i := 0; i < 10; i++ {
  580. if i%2 == 0 {
  581. for j := 0; j < 1e5; j++ {
  582. src[1e5*i+j] = uint8(rng.Intn(256))
  583. }
  584. } else {
  585. for j := 0; j < 1e5; j++ {
  586. src[1e5*i+j] = uint8(i)
  587. }
  588. }
  589. }
  590. buf := new(bytes.Buffer)
  591. if _, err := NewWriter(buf).Write(src); err != nil {
  592. t.Fatalf("Write: encoding: %v", err)
  593. }
  594. dst, err := ioutil.ReadAll(NewReader(buf))
  595. if err != nil {
  596. t.Fatalf("ReadAll: decoding: %v", err)
  597. }
  598. if err := cmp(dst, src); err != nil {
  599. t.Fatal(err)
  600. }
  601. }
  602. func TestWriterGoldenOutput(t *testing.T) {
  603. buf := new(bytes.Buffer)
  604. w := NewBufferedWriter(buf)
  605. defer w.Close()
  606. w.Write([]byte("abcd")) // Not compressible.
  607. w.Flush()
  608. w.Write(bytes.Repeat([]byte{'A'}, 150)) // Compressible.
  609. w.Flush()
  610. // The next chunk is also compressible, but a naive, greedy encoding of the
  611. // overall length 67 copy as a length 64 copy (the longest expressible as a
  612. // tagCopy1 or tagCopy2) plus a length 3 remainder would be two 3-byte
  613. // tagCopy2 tags (6 bytes), since the minimum length for a tagCopy1 is 4
  614. // bytes. Instead, we could do it shorter, in 5 bytes: a 3-byte tagCopy2
  615. // (of length 60) and a 2-byte tagCopy1 (of length 7).
  616. w.Write(bytes.Repeat([]byte{'B'}, 68))
  617. w.Flush()
  618. got := buf.String()
  619. want := strings.Join([]string{
  620. magicChunk,
  621. "\x01\x08\x00\x00", // Uncompressed chunk, 8 bytes long (including 4 byte checksum).
  622. "\x68\x10\xe6\xb6", // Checksum.
  623. "\x61\x62\x63\x64", // Uncompressed payload: "abcd".
  624. "\x00\x11\x00\x00", // Compressed chunk, 17 bytes long (including 4 byte checksum).
  625. "\x5f\xeb\xf2\x10", // Checksum.
  626. "\x96\x01", // Compressed payload: Uncompressed length (varint encoded): 150.
  627. "\x00\x41", // Compressed payload: tagLiteral, length=1, "A".
  628. "\xfe\x01\x00", // Compressed payload: tagCopy2, length=64, offset=1.
  629. "\xfe\x01\x00", // Compressed payload: tagCopy2, length=64, offset=1.
  630. "\x52\x01\x00", // Compressed payload: tagCopy2, length=21, offset=1.
  631. "\x00\x0c\x00\x00", // Compressed chunk, 12 bytes long (including 4 byte checksum).
  632. "\x27\x50\xe4\x4e", // Checksum.
  633. "\x44", // Compressed payload: Uncompressed length (varint encoded): 68.
  634. "\x00\x42", // Compressed payload: tagLiteral, length=1, "B".
  635. "\xee\x01\x00", // Compressed payload: tagCopy2, length=60, offset=1.
  636. "\x0d\x01", // Compressed payload: tagCopy1, length=7, offset=1.
  637. }, "")
  638. if got != want {
  639. t.Fatalf("\ngot: % x\nwant: % x", got, want)
  640. }
  641. }
  642. func TestNewBufferedWriter(t *testing.T) {
  643. // Test all 32 possible sub-sequences of these 5 input slices.
  644. //
  645. // Their lengths sum to 400,000, which is over 6 times the Writer ibuf
  646. // capacity: 6 * maxBlockSize is 393,216.
  647. inputs := [][]byte{
  648. bytes.Repeat([]byte{'a'}, 40000),
  649. bytes.Repeat([]byte{'b'}, 150000),
  650. bytes.Repeat([]byte{'c'}, 60000),
  651. bytes.Repeat([]byte{'d'}, 120000),
  652. bytes.Repeat([]byte{'e'}, 30000),
  653. }
  654. loop:
  655. for i := 0; i < 1<<uint(len(inputs)); i++ {
  656. var want []byte
  657. buf := new(bytes.Buffer)
  658. w := NewBufferedWriter(buf)
  659. for j, input := range inputs {
  660. if i&(1<<uint(j)) == 0 {
  661. continue
  662. }
  663. if _, err := w.Write(input); err != nil {
  664. t.Errorf("i=%#02x: j=%d: Write: %v", i, j, err)
  665. continue loop
  666. }
  667. want = append(want, input...)
  668. }
  669. if err := w.Close(); err != nil {
  670. t.Errorf("i=%#02x: Close: %v", i, err)
  671. continue
  672. }
  673. got, err := ioutil.ReadAll(NewReader(buf))
  674. if err != nil {
  675. t.Errorf("i=%#02x: ReadAll: %v", i, err)
  676. continue
  677. }
  678. if err := cmp(got, want); err != nil {
  679. t.Errorf("i=%#02x: %v", i, err)
  680. continue
  681. }
  682. }
  683. }
  684. func TestFlush(t *testing.T) {
  685. buf := new(bytes.Buffer)
  686. w := NewBufferedWriter(buf)
  687. defer w.Close()
  688. if _, err := w.Write(bytes.Repeat([]byte{'x'}, 20)); err != nil {
  689. t.Fatalf("Write: %v", err)
  690. }
  691. if n := buf.Len(); n != 0 {
  692. t.Fatalf("before Flush: %d bytes were written to the underlying io.Writer, want 0", n)
  693. }
  694. if err := w.Flush(); err != nil {
  695. t.Fatalf("Flush: %v", err)
  696. }
  697. if n := buf.Len(); n == 0 {
  698. t.Fatalf("after Flush: %d bytes were written to the underlying io.Writer, want non-0", n)
  699. }
  700. }
  701. func TestReaderUncompressedDataOK(t *testing.T) {
  702. r := NewReader(strings.NewReader(magicChunk +
  703. "\x01\x08\x00\x00" + // Uncompressed chunk, 8 bytes long (including 4 byte checksum).
  704. "\x68\x10\xe6\xb6" + // Checksum.
  705. "\x61\x62\x63\x64", // Uncompressed payload: "abcd".
  706. ))
  707. g, err := ioutil.ReadAll(r)
  708. if err != nil {
  709. t.Fatal(err)
  710. }
  711. if got, want := string(g), "abcd"; got != want {
  712. t.Fatalf("got %q, want %q", got, want)
  713. }
  714. }
  715. func TestReaderUncompressedDataNoPayload(t *testing.T) {
  716. r := NewReader(strings.NewReader(magicChunk +
  717. "\x01\x04\x00\x00" + // Uncompressed chunk, 4 bytes long.
  718. "", // No payload; corrupt input.
  719. ))
  720. if _, err := ioutil.ReadAll(r); err != ErrCorrupt {
  721. t.Fatalf("got %v, want %v", err, ErrCorrupt)
  722. }
  723. }
  724. func TestReaderUncompressedDataTooLong(t *testing.T) {
  725. // https://github.com/google/snappy/blob/master/framing_format.txt section
  726. // 4.3 says that "the maximum legal chunk length... is 65540", or 0x10004.
  727. const n = 0x10005
  728. r := NewReader(strings.NewReader(magicChunk +
  729. "\x01\x05\x00\x01" + // Uncompressed chunk, n bytes long.
  730. strings.Repeat("\x00", n),
  731. ))
  732. if _, err := ioutil.ReadAll(r); err != ErrCorrupt {
  733. t.Fatalf("got %v, want %v", err, ErrCorrupt)
  734. }
  735. }
  736. func TestReaderReset(t *testing.T) {
  737. gold := bytes.Repeat([]byte("All that is gold does not glitter,\n"), 10000)
  738. buf := new(bytes.Buffer)
  739. if _, err := NewWriter(buf).Write(gold); err != nil {
  740. t.Fatalf("Write: %v", err)
  741. }
  742. encoded, invalid, partial := buf.String(), "invalid", "partial"
  743. r := NewReader(nil)
  744. for i, s := range []string{encoded, invalid, partial, encoded, partial, invalid, encoded, encoded} {
  745. if s == partial {
  746. r.Reset(strings.NewReader(encoded))
  747. if _, err := r.Read(make([]byte, 101)); err != nil {
  748. t.Errorf("#%d: %v", i, err)
  749. continue
  750. }
  751. continue
  752. }
  753. r.Reset(strings.NewReader(s))
  754. got, err := ioutil.ReadAll(r)
  755. switch s {
  756. case encoded:
  757. if err != nil {
  758. t.Errorf("#%d: %v", i, err)
  759. continue
  760. }
  761. if err := cmp(got, gold); err != nil {
  762. t.Errorf("#%d: %v", i, err)
  763. continue
  764. }
  765. case invalid:
  766. if err == nil {
  767. t.Errorf("#%d: got nil error, want non-nil", i)
  768. continue
  769. }
  770. }
  771. }
  772. }
  773. func TestWriterReset(t *testing.T) {
  774. gold := bytes.Repeat([]byte("Not all those who wander are lost;\n"), 10000)
  775. const n = 20
  776. for _, buffered := range []bool{false, true} {
  777. var w *Writer
  778. if buffered {
  779. w = NewBufferedWriter(nil)
  780. defer w.Close()
  781. } else {
  782. w = NewWriter(nil)
  783. }
  784. var gots, wants [][]byte
  785. failed := false
  786. for i := 0; i <= n; i++ {
  787. buf := new(bytes.Buffer)
  788. w.Reset(buf)
  789. want := gold[:len(gold)*i/n]
  790. if _, err := w.Write(want); err != nil {
  791. t.Errorf("#%d: Write: %v", i, err)
  792. failed = true
  793. continue
  794. }
  795. if buffered {
  796. if err := w.Flush(); err != nil {
  797. t.Errorf("#%d: Flush: %v", i, err)
  798. failed = true
  799. continue
  800. }
  801. }
  802. got, err := ioutil.ReadAll(NewReader(buf))
  803. if err != nil {
  804. t.Errorf("#%d: ReadAll: %v", i, err)
  805. failed = true
  806. continue
  807. }
  808. gots = append(gots, got)
  809. wants = append(wants, want)
  810. }
  811. if failed {
  812. continue
  813. }
  814. for i := range gots {
  815. if err := cmp(gots[i], wants[i]); err != nil {
  816. t.Errorf("#%d: %v", i, err)
  817. }
  818. }
  819. }
  820. }
  821. func TestWriterResetWithoutFlush(t *testing.T) {
  822. buf0 := new(bytes.Buffer)
  823. buf1 := new(bytes.Buffer)
  824. w := NewBufferedWriter(buf0)
  825. if _, err := w.Write([]byte("xxx")); err != nil {
  826. t.Fatalf("Write #0: %v", err)
  827. }
  828. // Note that we don't Flush the Writer before calling Reset.
  829. w.Reset(buf1)
  830. if _, err := w.Write([]byte("yyy")); err != nil {
  831. t.Fatalf("Write #1: %v", err)
  832. }
  833. if err := w.Flush(); err != nil {
  834. t.Fatalf("Flush: %v", err)
  835. }
  836. got, err := ioutil.ReadAll(NewReader(buf1))
  837. if err != nil {
  838. t.Fatalf("ReadAll: %v", err)
  839. }
  840. if err := cmp(got, []byte("yyy")); err != nil {
  841. t.Fatal(err)
  842. }
  843. }
  844. type writeCounter int
  845. func (c *writeCounter) Write(p []byte) (int, error) {
  846. *c++
  847. return len(p), nil
  848. }
  849. // TestNumUnderlyingWrites tests that each Writer flush only makes one or two
  850. // Write calls on its underlying io.Writer, depending on whether or not the
  851. // flushed buffer was compressible.
  852. func TestNumUnderlyingWrites(t *testing.T) {
  853. testCases := []struct {
  854. input []byte
  855. want int
  856. }{
  857. {bytes.Repeat([]byte{'x'}, 100), 1},
  858. {bytes.Repeat([]byte{'y'}, 100), 1},
  859. {[]byte("ABCDEFGHIJKLMNOPQRST"), 2},
  860. }
  861. var c writeCounter
  862. w := NewBufferedWriter(&c)
  863. defer w.Close()
  864. for i, tc := range testCases {
  865. c = 0
  866. if _, err := w.Write(tc.input); err != nil {
  867. t.Errorf("#%d: Write: %v", i, err)
  868. continue
  869. }
  870. if err := w.Flush(); err != nil {
  871. t.Errorf("#%d: Flush: %v", i, err)
  872. continue
  873. }
  874. if int(c) != tc.want {
  875. t.Errorf("#%d: got %d underlying writes, want %d", i, c, tc.want)
  876. continue
  877. }
  878. }
  879. }
  880. func benchDecode(b *testing.B, src []byte) {
  881. encoded := Encode(nil, src)
  882. // Bandwidth is in amount of uncompressed data.
  883. b.SetBytes(int64(len(src)))
  884. b.ResetTimer()
  885. for i := 0; i < b.N; i++ {
  886. Decode(src, encoded)
  887. }
  888. }
  889. func benchEncode(b *testing.B, src []byte) {
  890. // Bandwidth is in amount of uncompressed data.
  891. b.SetBytes(int64(len(src)))
  892. dst := make([]byte, MaxEncodedLen(len(src)))
  893. b.ResetTimer()
  894. for i := 0; i < b.N; i++ {
  895. Encode(dst, src)
  896. }
  897. }
  898. func testOrBenchmark(b testing.TB) string {
  899. if _, ok := b.(*testing.B); ok {
  900. return "benchmark"
  901. }
  902. return "test"
  903. }
  904. func readFile(b testing.TB, filename string) []byte {
  905. src, err := ioutil.ReadFile(filename)
  906. if err != nil {
  907. b.Skipf("skipping %s: %v", testOrBenchmark(b), err)
  908. }
  909. if len(src) == 0 {
  910. b.Fatalf("%s has zero length", filename)
  911. }
  912. return src
  913. }
  914. // expand returns a slice of length n containing repeated copies of src.
  915. func expand(src []byte, n int) []byte {
  916. dst := make([]byte, n)
  917. for x := dst; len(x) > 0; {
  918. i := copy(x, src)
  919. x = x[i:]
  920. }
  921. return dst
  922. }
  923. func benchWords(b *testing.B, n int, decode bool) {
  924. // Note: the file is OS-language dependent so the resulting values are not
  925. // directly comparable for non-US-English OS installations.
  926. data := expand(readFile(b, "/usr/share/dict/words"), n)
  927. if decode {
  928. benchDecode(b, data)
  929. } else {
  930. benchEncode(b, data)
  931. }
  932. }
  933. func BenchmarkWordsDecode1e1(b *testing.B) { benchWords(b, 1e1, true) }
  934. func BenchmarkWordsDecode1e2(b *testing.B) { benchWords(b, 1e2, true) }
  935. func BenchmarkWordsDecode1e3(b *testing.B) { benchWords(b, 1e3, true) }
  936. func BenchmarkWordsDecode1e4(b *testing.B) { benchWords(b, 1e4, true) }
  937. func BenchmarkWordsDecode1e5(b *testing.B) { benchWords(b, 1e5, true) }
  938. func BenchmarkWordsDecode1e6(b *testing.B) { benchWords(b, 1e6, true) }
  939. func BenchmarkWordsEncode1e1(b *testing.B) { benchWords(b, 1e1, false) }
  940. func BenchmarkWordsEncode1e2(b *testing.B) { benchWords(b, 1e2, false) }
  941. func BenchmarkWordsEncode1e3(b *testing.B) { benchWords(b, 1e3, false) }
  942. func BenchmarkWordsEncode1e4(b *testing.B) { benchWords(b, 1e4, false) }
  943. func BenchmarkWordsEncode1e5(b *testing.B) { benchWords(b, 1e5, false) }
  944. func BenchmarkWordsEncode1e6(b *testing.B) { benchWords(b, 1e6, false) }
  945. func BenchmarkRandomEncode(b *testing.B) {
  946. rng := rand.New(rand.NewSource(1))
  947. data := make([]byte, 1<<20)
  948. for i := range data {
  949. data[i] = uint8(rng.Intn(256))
  950. }
  951. benchEncode(b, data)
  952. }
  953. // testFiles' values are copied directly from
  954. // https://raw.githubusercontent.com/google/snappy/master/snappy_unittest.cc
  955. // The label field is unused in snappy-go.
  956. var testFiles = []struct {
  957. label string
  958. filename string
  959. sizeLimit int
  960. }{
  961. {"html", "html", 0},
  962. {"urls", "urls.10K", 0},
  963. {"jpg", "fireworks.jpeg", 0},
  964. {"jpg_200", "fireworks.jpeg", 200},
  965. {"pdf", "paper-100k.pdf", 0},
  966. {"html4", "html_x_4", 0},
  967. {"txt1", "alice29.txt", 0},
  968. {"txt2", "asyoulik.txt", 0},
  969. {"txt3", "lcet10.txt", 0},
  970. {"txt4", "plrabn12.txt", 0},
  971. {"pb", "geo.protodata", 0},
  972. {"gaviota", "kppkn.gtb", 0},
  973. }
  974. const (
  975. // The benchmark data files are at this canonical URL.
  976. benchURL = "https://raw.githubusercontent.com/google/snappy/master/testdata/"
  977. // They are copied to this local directory.
  978. benchDir = "testdata/bench"
  979. )
  980. func downloadBenchmarkFiles(b testing.TB, basename string) (errRet error) {
  981. filename := filepath.Join(benchDir, basename)
  982. if stat, err := os.Stat(filename); err == nil && stat.Size() != 0 {
  983. return nil
  984. }
  985. if !*download {
  986. b.Skipf("test data not found; skipping %s without the -download flag", testOrBenchmark(b))
  987. }
  988. // Download the official snappy C++ implementation reference test data
  989. // files for benchmarking.
  990. if err := os.MkdirAll(benchDir, 0777); err != nil && !os.IsExist(err) {
  991. return fmt.Errorf("failed to create %s: %s", benchDir, err)
  992. }
  993. f, err := os.Create(filename)
  994. if err != nil {
  995. return fmt.Errorf("failed to create %s: %s", filename, err)
  996. }
  997. defer f.Close()
  998. defer func() {
  999. if errRet != nil {
  1000. os.Remove(filename)
  1001. }
  1002. }()
  1003. url := benchURL + basename
  1004. resp, err := http.Get(url)
  1005. if err != nil {
  1006. return fmt.Errorf("failed to download %s: %s", url, err)
  1007. }
  1008. defer resp.Body.Close()
  1009. if s := resp.StatusCode; s != http.StatusOK {
  1010. return fmt.Errorf("downloading %s: HTTP status code %d (%s)", url, s, http.StatusText(s))
  1011. }
  1012. _, err = io.Copy(f, resp.Body)
  1013. if err != nil {
  1014. return fmt.Errorf("failed to download %s to %s: %s", url, filename, err)
  1015. }
  1016. return nil
  1017. }
  1018. func benchFile(b *testing.B, i int, decode bool) {
  1019. if err := downloadBenchmarkFiles(b, testFiles[i].filename); err != nil {
  1020. b.Fatalf("failed to download testdata: %s", err)
  1021. }
  1022. data := readFile(b, filepath.Join(benchDir, testFiles[i].filename))
  1023. if n := testFiles[i].sizeLimit; 0 < n && n < len(data) {
  1024. data = data[:n]
  1025. }
  1026. if decode {
  1027. benchDecode(b, data)
  1028. } else {
  1029. benchEncode(b, data)
  1030. }
  1031. }
  1032. // Naming convention is kept similar to what snappy's C++ implementation uses.
  1033. func Benchmark_UFlat0(b *testing.B) { benchFile(b, 0, true) }
  1034. func Benchmark_UFlat1(b *testing.B) { benchFile(b, 1, true) }
  1035. func Benchmark_UFlat2(b *testing.B) { benchFile(b, 2, true) }
  1036. func Benchmark_UFlat3(b *testing.B) { benchFile(b, 3, true) }
  1037. func Benchmark_UFlat4(b *testing.B) { benchFile(b, 4, true) }
  1038. func Benchmark_UFlat5(b *testing.B) { benchFile(b, 5, true) }
  1039. func Benchmark_UFlat6(b *testing.B) { benchFile(b, 6, true) }
  1040. func Benchmark_UFlat7(b *testing.B) { benchFile(b, 7, true) }
  1041. func Benchmark_UFlat8(b *testing.B) { benchFile(b, 8, true) }
  1042. func Benchmark_UFlat9(b *testing.B) { benchFile(b, 9, true) }
  1043. func Benchmark_UFlat10(b *testing.B) { benchFile(b, 10, true) }
  1044. func Benchmark_UFlat11(b *testing.B) { benchFile(b, 11, true) }
  1045. func Benchmark_ZFlat0(b *testing.B) { benchFile(b, 0, false) }
  1046. func Benchmark_ZFlat1(b *testing.B) { benchFile(b, 1, false) }
  1047. func Benchmark_ZFlat2(b *testing.B) { benchFile(b, 2, false) }
  1048. func Benchmark_ZFlat3(b *testing.B) { benchFile(b, 3, false) }
  1049. func Benchmark_ZFlat4(b *testing.B) { benchFile(b, 4, false) }
  1050. func Benchmark_ZFlat5(b *testing.B) { benchFile(b, 5, false) }
  1051. func Benchmark_ZFlat6(b *testing.B) { benchFile(b, 6, false) }
  1052. func Benchmark_ZFlat7(b *testing.B) { benchFile(b, 7, false) }
  1053. func Benchmark_ZFlat8(b *testing.B) { benchFile(b, 8, false) }
  1054. func Benchmark_ZFlat9(b *testing.B) { benchFile(b, 9, false) }
  1055. func Benchmark_ZFlat10(b *testing.B) { benchFile(b, 10, false) }
  1056. func Benchmark_ZFlat11(b *testing.B) { benchFile(b, 11, false) }