snappy_test.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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. // TODO: pi.txt no longer compresses at all, after the Go code implemented the
  436. // same algorithmic change as the C++ code:
  437. // https://github.com/google/snappy/commit/d53de18799418e113e44444252a39b12a0e4e0cc
  438. //
  439. // We should use a more compressible test file.
  440. func TestDecodeGoldenInput(t *testing.T) {
  441. src, err := ioutil.ReadFile("testdata/pi.txt.rawsnappy")
  442. if err != nil {
  443. t.Fatalf("ReadFile: %v", err)
  444. }
  445. got, err := Decode(nil, src)
  446. if err != nil {
  447. t.Fatalf("Decode: %v", err)
  448. }
  449. want, err := ioutil.ReadFile("testdata/pi.txt")
  450. if err != nil {
  451. t.Fatalf("ReadFile: %v", err)
  452. }
  453. if err := cmp(got, want); err != nil {
  454. t.Fatal(err)
  455. }
  456. }
  457. func TestEncodeGoldenInput(t *testing.T) {
  458. src, err := ioutil.ReadFile("testdata/pi.txt")
  459. if err != nil {
  460. t.Fatalf("ReadFile: %v", err)
  461. }
  462. got := Encode(nil, src)
  463. want, err := ioutil.ReadFile("testdata/pi.txt.rawsnappy")
  464. if err != nil {
  465. t.Fatalf("ReadFile: %v", err)
  466. }
  467. if err := cmp(got, want); err != nil {
  468. t.Fatal(err)
  469. }
  470. }
  471. const snappytoolCmdName = "cmd/snappytool/snappytool"
  472. func skipTestSameEncodingAsCpp() (msg string) {
  473. if !goEncoderShouldMatchCppEncoder {
  474. return fmt.Sprintf("skipping testing that the encoding is byte-for-byte identical to C++: GOARCH=%s", runtime.GOARCH)
  475. }
  476. if _, err := os.Stat(snappytoolCmdName); err != nil {
  477. return fmt.Sprintf("could not find snappytool: %v", err)
  478. }
  479. return ""
  480. }
  481. func runTestSameEncodingAsCpp(src []byte) error {
  482. got := Encode(nil, src)
  483. cmd := exec.Command(snappytoolCmdName, "-e")
  484. cmd.Stdin = bytes.NewReader(src)
  485. want, err := cmd.Output()
  486. if err != nil {
  487. return fmt.Errorf("could not run snappytool: %v", err)
  488. }
  489. return cmp(got, want)
  490. }
  491. func TestSameEncodingAsCppShortCopies(t *testing.T) {
  492. if msg := skipTestSameEncodingAsCpp(); msg != "" {
  493. t.Skip(msg)
  494. }
  495. src := bytes.Repeat([]byte{'a'}, 20)
  496. for i := 0; i <= len(src); i++ {
  497. if err := runTestSameEncodingAsCpp(src[:i]); err != nil {
  498. t.Errorf("i=%d: %v", i, err)
  499. }
  500. }
  501. }
  502. func TestSameEncodingAsCppLongFiles(t *testing.T) {
  503. if msg := skipTestSameEncodingAsCpp(); msg != "" {
  504. t.Skip(msg)
  505. }
  506. failed := false
  507. for i, tf := range testFiles {
  508. if err := downloadBenchmarkFiles(t, tf.filename); err != nil {
  509. t.Fatalf("failed to download testdata: %s", err)
  510. }
  511. data := readFile(t, filepath.Join(benchDir, tf.filename))
  512. if n := tf.sizeLimit; 0 < n && n < len(data) {
  513. data = data[:n]
  514. }
  515. if err := runTestSameEncodingAsCpp(data); err != nil {
  516. t.Errorf("i=%d: %v", i, err)
  517. failed = true
  518. }
  519. }
  520. if failed {
  521. t.Errorf("was the snappytool program built against the C++ snappy library version " +
  522. "d53de187 or later, commited on 2016-04-05? See " +
  523. "https://github.com/google/snappy/commit/d53de18799418e113e44444252a39b12a0e4e0cc")
  524. }
  525. }
  526. // TestSlowForwardCopyOverrun tests the "expand the pattern" algorithm
  527. // described in decode_amd64.s and its claim of a 10 byte overrun worst case.
  528. func TestSlowForwardCopyOverrun(t *testing.T) {
  529. const base = 100
  530. for length := 1; length < 18; length++ {
  531. for offset := 1; offset < 18; offset++ {
  532. highWaterMark := base
  533. d := base
  534. l := length
  535. o := offset
  536. // makeOffsetAtLeast8
  537. for o < 8 {
  538. if end := d + 8; highWaterMark < end {
  539. highWaterMark = end
  540. }
  541. l -= o
  542. d += o
  543. o += o
  544. }
  545. // fixUpSlowForwardCopy
  546. a := d
  547. d += l
  548. // finishSlowForwardCopy
  549. for l > 0 {
  550. if end := a + 8; highWaterMark < end {
  551. highWaterMark = end
  552. }
  553. a += 8
  554. l -= 8
  555. }
  556. dWant := base + length
  557. overrun := highWaterMark - dWant
  558. if d != dWant || overrun < 0 || 10 < overrun {
  559. t.Errorf("length=%d, offset=%d: d and overrun: got (%d, %d), want (%d, something in [0, 10])",
  560. length, offset, d, overrun, dWant)
  561. }
  562. }
  563. }
  564. }
  565. // TestEncodeNoiseThenRepeats encodes input for which the first half is very
  566. // incompressible and the second half is very compressible. The encoded form's
  567. // length should be closer to 50% of the original length than 100%.
  568. func TestEncodeNoiseThenRepeats(t *testing.T) {
  569. for _, origLen := range []int{256 * 1024, 2048 * 1024} {
  570. src := make([]byte, origLen)
  571. rng := rand.New(rand.NewSource(1))
  572. firstHalf, secondHalf := src[:origLen/2], src[origLen/2:]
  573. for i := range firstHalf {
  574. firstHalf[i] = uint8(rng.Intn(256))
  575. }
  576. for i := range secondHalf {
  577. secondHalf[i] = uint8(i >> 8)
  578. }
  579. dst := Encode(nil, src)
  580. if got, want := len(dst), origLen*3/4; got >= want {
  581. t.Errorf("origLen=%d: got %d encoded bytes, want less than %d", origLen, got, want)
  582. }
  583. }
  584. }
  585. func TestFramingFormat(t *testing.T) {
  586. // src is comprised of alternating 1e5-sized sequences of random
  587. // (incompressible) bytes and repeated (compressible) bytes. 1e5 was chosen
  588. // because it is larger than maxBlockSize (64k).
  589. src := make([]byte, 1e6)
  590. rng := rand.New(rand.NewSource(1))
  591. for i := 0; i < 10; i++ {
  592. if i%2 == 0 {
  593. for j := 0; j < 1e5; j++ {
  594. src[1e5*i+j] = uint8(rng.Intn(256))
  595. }
  596. } else {
  597. for j := 0; j < 1e5; j++ {
  598. src[1e5*i+j] = uint8(i)
  599. }
  600. }
  601. }
  602. buf := new(bytes.Buffer)
  603. if _, err := NewWriter(buf).Write(src); err != nil {
  604. t.Fatalf("Write: encoding: %v", err)
  605. }
  606. dst, err := ioutil.ReadAll(NewReader(buf))
  607. if err != nil {
  608. t.Fatalf("ReadAll: decoding: %v", err)
  609. }
  610. if err := cmp(dst, src); err != nil {
  611. t.Fatal(err)
  612. }
  613. }
  614. func TestWriterGoldenOutput(t *testing.T) {
  615. buf := new(bytes.Buffer)
  616. w := NewBufferedWriter(buf)
  617. defer w.Close()
  618. w.Write([]byte("abcd")) // Not compressible.
  619. w.Flush()
  620. w.Write(bytes.Repeat([]byte{'A'}, 150)) // Compressible.
  621. w.Flush()
  622. // The next chunk is also compressible, but a naive, greedy encoding of the
  623. // overall length 67 copy as a length 64 copy (the longest expressible as a
  624. // tagCopy1 or tagCopy2) plus a length 3 remainder would be two 3-byte
  625. // tagCopy2 tags (6 bytes), since the minimum length for a tagCopy1 is 4
  626. // bytes. Instead, we could do it shorter, in 5 bytes: a 3-byte tagCopy2
  627. // (of length 60) and a 2-byte tagCopy1 (of length 7).
  628. w.Write(bytes.Repeat([]byte{'B'}, 68))
  629. w.Flush()
  630. got := buf.String()
  631. want := strings.Join([]string{
  632. magicChunk,
  633. "\x01\x08\x00\x00", // Uncompressed chunk, 8 bytes long (including 4 byte checksum).
  634. "\x68\x10\xe6\xb6", // Checksum.
  635. "\x61\x62\x63\x64", // Uncompressed payload: "abcd".
  636. "\x00\x11\x00\x00", // Compressed chunk, 17 bytes long (including 4 byte checksum).
  637. "\x5f\xeb\xf2\x10", // Checksum.
  638. "\x96\x01", // Compressed payload: Uncompressed length (varint encoded): 150.
  639. "\x00\x41", // Compressed payload: tagLiteral, length=1, "A".
  640. "\xfe\x01\x00", // Compressed payload: tagCopy2, length=64, offset=1.
  641. "\xfe\x01\x00", // Compressed payload: tagCopy2, length=64, offset=1.
  642. "\x52\x01\x00", // Compressed payload: tagCopy2, length=21, offset=1.
  643. "\x00\x0c\x00\x00", // Compressed chunk, 12 bytes long (including 4 byte checksum).
  644. "\x27\x50\xe4\x4e", // Checksum.
  645. "\x44", // Compressed payload: Uncompressed length (varint encoded): 68.
  646. "\x00\x42", // Compressed payload: tagLiteral, length=1, "B".
  647. "\xee\x01\x00", // Compressed payload: tagCopy2, length=60, offset=1.
  648. "\x0d\x01", // Compressed payload: tagCopy1, length=7, offset=1.
  649. }, "")
  650. if got != want {
  651. t.Fatalf("\ngot: % x\nwant: % x", got, want)
  652. }
  653. }
  654. func TestNewBufferedWriter(t *testing.T) {
  655. // Test all 32 possible sub-sequences of these 5 input slices.
  656. //
  657. // Their lengths sum to 400,000, which is over 6 times the Writer ibuf
  658. // capacity: 6 * maxBlockSize is 393,216.
  659. inputs := [][]byte{
  660. bytes.Repeat([]byte{'a'}, 40000),
  661. bytes.Repeat([]byte{'b'}, 150000),
  662. bytes.Repeat([]byte{'c'}, 60000),
  663. bytes.Repeat([]byte{'d'}, 120000),
  664. bytes.Repeat([]byte{'e'}, 30000),
  665. }
  666. loop:
  667. for i := 0; i < 1<<uint(len(inputs)); i++ {
  668. var want []byte
  669. buf := new(bytes.Buffer)
  670. w := NewBufferedWriter(buf)
  671. for j, input := range inputs {
  672. if i&(1<<uint(j)) == 0 {
  673. continue
  674. }
  675. if _, err := w.Write(input); err != nil {
  676. t.Errorf("i=%#02x: j=%d: Write: %v", i, j, err)
  677. continue loop
  678. }
  679. want = append(want, input...)
  680. }
  681. if err := w.Close(); err != nil {
  682. t.Errorf("i=%#02x: Close: %v", i, err)
  683. continue
  684. }
  685. got, err := ioutil.ReadAll(NewReader(buf))
  686. if err != nil {
  687. t.Errorf("i=%#02x: ReadAll: %v", i, err)
  688. continue
  689. }
  690. if err := cmp(got, want); err != nil {
  691. t.Errorf("i=%#02x: %v", i, err)
  692. continue
  693. }
  694. }
  695. }
  696. func TestFlush(t *testing.T) {
  697. buf := new(bytes.Buffer)
  698. w := NewBufferedWriter(buf)
  699. defer w.Close()
  700. if _, err := w.Write(bytes.Repeat([]byte{'x'}, 20)); err != nil {
  701. t.Fatalf("Write: %v", err)
  702. }
  703. if n := buf.Len(); n != 0 {
  704. t.Fatalf("before Flush: %d bytes were written to the underlying io.Writer, want 0", n)
  705. }
  706. if err := w.Flush(); err != nil {
  707. t.Fatalf("Flush: %v", err)
  708. }
  709. if n := buf.Len(); n == 0 {
  710. t.Fatalf("after Flush: %d bytes were written to the underlying io.Writer, want non-0", n)
  711. }
  712. }
  713. func TestReaderUncompressedDataOK(t *testing.T) {
  714. r := NewReader(strings.NewReader(magicChunk +
  715. "\x01\x08\x00\x00" + // Uncompressed chunk, 8 bytes long (including 4 byte checksum).
  716. "\x68\x10\xe6\xb6" + // Checksum.
  717. "\x61\x62\x63\x64", // Uncompressed payload: "abcd".
  718. ))
  719. g, err := ioutil.ReadAll(r)
  720. if err != nil {
  721. t.Fatal(err)
  722. }
  723. if got, want := string(g), "abcd"; got != want {
  724. t.Fatalf("got %q, want %q", got, want)
  725. }
  726. }
  727. func TestReaderUncompressedDataNoPayload(t *testing.T) {
  728. r := NewReader(strings.NewReader(magicChunk +
  729. "\x01\x04\x00\x00" + // Uncompressed chunk, 4 bytes long.
  730. "", // No payload; corrupt input.
  731. ))
  732. if _, err := ioutil.ReadAll(r); err != ErrCorrupt {
  733. t.Fatalf("got %v, want %v", err, ErrCorrupt)
  734. }
  735. }
  736. func TestReaderUncompressedDataTooLong(t *testing.T) {
  737. // https://github.com/google/snappy/blob/master/framing_format.txt section
  738. // 4.3 says that "the maximum legal chunk length... is 65540", or 0x10004.
  739. const n = 0x10005
  740. r := NewReader(strings.NewReader(magicChunk +
  741. "\x01\x05\x00\x01" + // Uncompressed chunk, n bytes long.
  742. strings.Repeat("\x00", n),
  743. ))
  744. if _, err := ioutil.ReadAll(r); err != ErrCorrupt {
  745. t.Fatalf("got %v, want %v", err, ErrCorrupt)
  746. }
  747. }
  748. func TestReaderReset(t *testing.T) {
  749. gold := bytes.Repeat([]byte("All that is gold does not glitter,\n"), 10000)
  750. buf := new(bytes.Buffer)
  751. if _, err := NewWriter(buf).Write(gold); err != nil {
  752. t.Fatalf("Write: %v", err)
  753. }
  754. encoded, invalid, partial := buf.String(), "invalid", "partial"
  755. r := NewReader(nil)
  756. for i, s := range []string{encoded, invalid, partial, encoded, partial, invalid, encoded, encoded} {
  757. if s == partial {
  758. r.Reset(strings.NewReader(encoded))
  759. if _, err := r.Read(make([]byte, 101)); err != nil {
  760. t.Errorf("#%d: %v", i, err)
  761. continue
  762. }
  763. continue
  764. }
  765. r.Reset(strings.NewReader(s))
  766. got, err := ioutil.ReadAll(r)
  767. switch s {
  768. case encoded:
  769. if err != nil {
  770. t.Errorf("#%d: %v", i, err)
  771. continue
  772. }
  773. if err := cmp(got, gold); err != nil {
  774. t.Errorf("#%d: %v", i, err)
  775. continue
  776. }
  777. case invalid:
  778. if err == nil {
  779. t.Errorf("#%d: got nil error, want non-nil", i)
  780. continue
  781. }
  782. }
  783. }
  784. }
  785. func TestWriterReset(t *testing.T) {
  786. gold := bytes.Repeat([]byte("Not all those who wander are lost;\n"), 10000)
  787. const n = 20
  788. for _, buffered := range []bool{false, true} {
  789. var w *Writer
  790. if buffered {
  791. w = NewBufferedWriter(nil)
  792. defer w.Close()
  793. } else {
  794. w = NewWriter(nil)
  795. }
  796. var gots, wants [][]byte
  797. failed := false
  798. for i := 0; i <= n; i++ {
  799. buf := new(bytes.Buffer)
  800. w.Reset(buf)
  801. want := gold[:len(gold)*i/n]
  802. if _, err := w.Write(want); err != nil {
  803. t.Errorf("#%d: Write: %v", i, err)
  804. failed = true
  805. continue
  806. }
  807. if buffered {
  808. if err := w.Flush(); err != nil {
  809. t.Errorf("#%d: Flush: %v", i, err)
  810. failed = true
  811. continue
  812. }
  813. }
  814. got, err := ioutil.ReadAll(NewReader(buf))
  815. if err != nil {
  816. t.Errorf("#%d: ReadAll: %v", i, err)
  817. failed = true
  818. continue
  819. }
  820. gots = append(gots, got)
  821. wants = append(wants, want)
  822. }
  823. if failed {
  824. continue
  825. }
  826. for i := range gots {
  827. if err := cmp(gots[i], wants[i]); err != nil {
  828. t.Errorf("#%d: %v", i, err)
  829. }
  830. }
  831. }
  832. }
  833. func TestWriterResetWithoutFlush(t *testing.T) {
  834. buf0 := new(bytes.Buffer)
  835. buf1 := new(bytes.Buffer)
  836. w := NewBufferedWriter(buf0)
  837. if _, err := w.Write([]byte("xxx")); err != nil {
  838. t.Fatalf("Write #0: %v", err)
  839. }
  840. // Note that we don't Flush the Writer before calling Reset.
  841. w.Reset(buf1)
  842. if _, err := w.Write([]byte("yyy")); err != nil {
  843. t.Fatalf("Write #1: %v", err)
  844. }
  845. if err := w.Flush(); err != nil {
  846. t.Fatalf("Flush: %v", err)
  847. }
  848. got, err := ioutil.ReadAll(NewReader(buf1))
  849. if err != nil {
  850. t.Fatalf("ReadAll: %v", err)
  851. }
  852. if err := cmp(got, []byte("yyy")); err != nil {
  853. t.Fatal(err)
  854. }
  855. }
  856. type writeCounter int
  857. func (c *writeCounter) Write(p []byte) (int, error) {
  858. *c++
  859. return len(p), nil
  860. }
  861. // TestNumUnderlyingWrites tests that each Writer flush only makes one or two
  862. // Write calls on its underlying io.Writer, depending on whether or not the
  863. // flushed buffer was compressible.
  864. func TestNumUnderlyingWrites(t *testing.T) {
  865. testCases := []struct {
  866. input []byte
  867. want int
  868. }{
  869. {bytes.Repeat([]byte{'x'}, 100), 1},
  870. {bytes.Repeat([]byte{'y'}, 100), 1},
  871. {[]byte("ABCDEFGHIJKLMNOPQRST"), 2},
  872. }
  873. var c writeCounter
  874. w := NewBufferedWriter(&c)
  875. defer w.Close()
  876. for i, tc := range testCases {
  877. c = 0
  878. if _, err := w.Write(tc.input); err != nil {
  879. t.Errorf("#%d: Write: %v", i, err)
  880. continue
  881. }
  882. if err := w.Flush(); err != nil {
  883. t.Errorf("#%d: Flush: %v", i, err)
  884. continue
  885. }
  886. if int(c) != tc.want {
  887. t.Errorf("#%d: got %d underlying writes, want %d", i, c, tc.want)
  888. continue
  889. }
  890. }
  891. }
  892. func benchDecode(b *testing.B, src []byte) {
  893. encoded := Encode(nil, src)
  894. // Bandwidth is in amount of uncompressed data.
  895. b.SetBytes(int64(len(src)))
  896. b.ResetTimer()
  897. for i := 0; i < b.N; i++ {
  898. Decode(src, encoded)
  899. }
  900. }
  901. func benchEncode(b *testing.B, src []byte) {
  902. // Bandwidth is in amount of uncompressed data.
  903. b.SetBytes(int64(len(src)))
  904. dst := make([]byte, MaxEncodedLen(len(src)))
  905. b.ResetTimer()
  906. for i := 0; i < b.N; i++ {
  907. Encode(dst, src)
  908. }
  909. }
  910. func testOrBenchmark(b testing.TB) string {
  911. if _, ok := b.(*testing.B); ok {
  912. return "benchmark"
  913. }
  914. return "test"
  915. }
  916. func readFile(b testing.TB, filename string) []byte {
  917. src, err := ioutil.ReadFile(filename)
  918. if err != nil {
  919. b.Skipf("skipping %s: %v", testOrBenchmark(b), err)
  920. }
  921. if len(src) == 0 {
  922. b.Fatalf("%s has zero length", filename)
  923. }
  924. return src
  925. }
  926. // expand returns a slice of length n containing repeated copies of src.
  927. func expand(src []byte, n int) []byte {
  928. dst := make([]byte, n)
  929. for x := dst; len(x) > 0; {
  930. i := copy(x, src)
  931. x = x[i:]
  932. }
  933. return dst
  934. }
  935. func benchWords(b *testing.B, n int, decode bool) {
  936. // Note: the file is OS-language dependent so the resulting values are not
  937. // directly comparable for non-US-English OS installations.
  938. data := expand(readFile(b, "/usr/share/dict/words"), n)
  939. if decode {
  940. benchDecode(b, data)
  941. } else {
  942. benchEncode(b, data)
  943. }
  944. }
  945. func BenchmarkWordsDecode1e1(b *testing.B) { benchWords(b, 1e1, true) }
  946. func BenchmarkWordsDecode1e2(b *testing.B) { benchWords(b, 1e2, true) }
  947. func BenchmarkWordsDecode1e3(b *testing.B) { benchWords(b, 1e3, true) }
  948. func BenchmarkWordsDecode1e4(b *testing.B) { benchWords(b, 1e4, true) }
  949. func BenchmarkWordsDecode1e5(b *testing.B) { benchWords(b, 1e5, true) }
  950. func BenchmarkWordsDecode1e6(b *testing.B) { benchWords(b, 1e6, true) }
  951. func BenchmarkWordsEncode1e1(b *testing.B) { benchWords(b, 1e1, false) }
  952. func BenchmarkWordsEncode1e2(b *testing.B) { benchWords(b, 1e2, false) }
  953. func BenchmarkWordsEncode1e3(b *testing.B) { benchWords(b, 1e3, false) }
  954. func BenchmarkWordsEncode1e4(b *testing.B) { benchWords(b, 1e4, false) }
  955. func BenchmarkWordsEncode1e5(b *testing.B) { benchWords(b, 1e5, false) }
  956. func BenchmarkWordsEncode1e6(b *testing.B) { benchWords(b, 1e6, false) }
  957. func BenchmarkRandomEncode(b *testing.B) {
  958. rng := rand.New(rand.NewSource(1))
  959. data := make([]byte, 1<<20)
  960. for i := range data {
  961. data[i] = uint8(rng.Intn(256))
  962. }
  963. benchEncode(b, data)
  964. }
  965. // testFiles' values are copied directly from
  966. // https://raw.githubusercontent.com/google/snappy/master/snappy_unittest.cc
  967. // The label field is unused in snappy-go.
  968. var testFiles = []struct {
  969. label string
  970. filename string
  971. sizeLimit int
  972. }{
  973. {"html", "html", 0},
  974. {"urls", "urls.10K", 0},
  975. {"jpg", "fireworks.jpeg", 0},
  976. {"jpg_200", "fireworks.jpeg", 200},
  977. {"pdf", "paper-100k.pdf", 0},
  978. {"html4", "html_x_4", 0},
  979. {"txt1", "alice29.txt", 0},
  980. {"txt2", "asyoulik.txt", 0},
  981. {"txt3", "lcet10.txt", 0},
  982. {"txt4", "plrabn12.txt", 0},
  983. {"pb", "geo.protodata", 0},
  984. {"gaviota", "kppkn.gtb", 0},
  985. }
  986. const (
  987. // The benchmark data files are at this canonical URL.
  988. benchURL = "https://raw.githubusercontent.com/google/snappy/master/testdata/"
  989. // They are copied to this local directory.
  990. benchDir = "testdata/bench"
  991. )
  992. func downloadBenchmarkFiles(b testing.TB, basename string) (errRet error) {
  993. filename := filepath.Join(benchDir, basename)
  994. if stat, err := os.Stat(filename); err == nil && stat.Size() != 0 {
  995. return nil
  996. }
  997. if !*download {
  998. b.Skipf("test data not found; skipping %s without the -download flag", testOrBenchmark(b))
  999. }
  1000. // Download the official snappy C++ implementation reference test data
  1001. // files for benchmarking.
  1002. if err := os.MkdirAll(benchDir, 0777); err != nil && !os.IsExist(err) {
  1003. return fmt.Errorf("failed to create %s: %s", benchDir, err)
  1004. }
  1005. f, err := os.Create(filename)
  1006. if err != nil {
  1007. return fmt.Errorf("failed to create %s: %s", filename, err)
  1008. }
  1009. defer f.Close()
  1010. defer func() {
  1011. if errRet != nil {
  1012. os.Remove(filename)
  1013. }
  1014. }()
  1015. url := benchURL + basename
  1016. resp, err := http.Get(url)
  1017. if err != nil {
  1018. return fmt.Errorf("failed to download %s: %s", url, err)
  1019. }
  1020. defer resp.Body.Close()
  1021. if s := resp.StatusCode; s != http.StatusOK {
  1022. return fmt.Errorf("downloading %s: HTTP status code %d (%s)", url, s, http.StatusText(s))
  1023. }
  1024. _, err = io.Copy(f, resp.Body)
  1025. if err != nil {
  1026. return fmt.Errorf("failed to download %s to %s: %s", url, filename, err)
  1027. }
  1028. return nil
  1029. }
  1030. func benchFile(b *testing.B, i int, decode bool) {
  1031. if err := downloadBenchmarkFiles(b, testFiles[i].filename); err != nil {
  1032. b.Fatalf("failed to download testdata: %s", err)
  1033. }
  1034. data := readFile(b, filepath.Join(benchDir, testFiles[i].filename))
  1035. if n := testFiles[i].sizeLimit; 0 < n && n < len(data) {
  1036. data = data[:n]
  1037. }
  1038. if decode {
  1039. benchDecode(b, data)
  1040. } else {
  1041. benchEncode(b, data)
  1042. }
  1043. }
  1044. // Naming convention is kept similar to what snappy's C++ implementation uses.
  1045. func Benchmark_UFlat0(b *testing.B) { benchFile(b, 0, true) }
  1046. func Benchmark_UFlat1(b *testing.B) { benchFile(b, 1, true) }
  1047. func Benchmark_UFlat2(b *testing.B) { benchFile(b, 2, true) }
  1048. func Benchmark_UFlat3(b *testing.B) { benchFile(b, 3, true) }
  1049. func Benchmark_UFlat4(b *testing.B) { benchFile(b, 4, true) }
  1050. func Benchmark_UFlat5(b *testing.B) { benchFile(b, 5, true) }
  1051. func Benchmark_UFlat6(b *testing.B) { benchFile(b, 6, true) }
  1052. func Benchmark_UFlat7(b *testing.B) { benchFile(b, 7, true) }
  1053. func Benchmark_UFlat8(b *testing.B) { benchFile(b, 8, true) }
  1054. func Benchmark_UFlat9(b *testing.B) { benchFile(b, 9, true) }
  1055. func Benchmark_UFlat10(b *testing.B) { benchFile(b, 10, true) }
  1056. func Benchmark_UFlat11(b *testing.B) { benchFile(b, 11, true) }
  1057. func Benchmark_ZFlat0(b *testing.B) { benchFile(b, 0, false) }
  1058. func Benchmark_ZFlat1(b *testing.B) { benchFile(b, 1, false) }
  1059. func Benchmark_ZFlat2(b *testing.B) { benchFile(b, 2, false) }
  1060. func Benchmark_ZFlat3(b *testing.B) { benchFile(b, 3, false) }
  1061. func Benchmark_ZFlat4(b *testing.B) { benchFile(b, 4, false) }
  1062. func Benchmark_ZFlat5(b *testing.B) { benchFile(b, 5, false) }
  1063. func Benchmark_ZFlat6(b *testing.B) { benchFile(b, 6, false) }
  1064. func Benchmark_ZFlat7(b *testing.B) { benchFile(b, 7, false) }
  1065. func Benchmark_ZFlat8(b *testing.B) { benchFile(b, 8, false) }
  1066. func Benchmark_ZFlat9(b *testing.B) { benchFile(b, 9, false) }
  1067. func Benchmark_ZFlat10(b *testing.B) { benchFile(b, 10, false) }
  1068. func Benchmark_ZFlat11(b *testing.B) { benchFile(b, 11, false) }