dict_decoder_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2016 The 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 flate
  5. import (
  6. "bytes"
  7. "strings"
  8. "testing"
  9. )
  10. func TestDictDecoder(t *testing.T) {
  11. const (
  12. abc = "ABC\n"
  13. fox = "The quick brown fox jumped over the lazy dog!\n"
  14. poem = "The Road Not Taken\nRobert Frost\n" +
  15. "\n" +
  16. "Two roads diverged in a yellow wood,\n" +
  17. "And sorry I could not travel both\n" +
  18. "And be one traveler, long I stood\n" +
  19. "And looked down one as far as I could\n" +
  20. "To where it bent in the undergrowth;\n" +
  21. "\n" +
  22. "Then took the other, as just as fair,\n" +
  23. "And having perhaps the better claim,\n" +
  24. "Because it was grassy and wanted wear;\n" +
  25. "Though as for that the passing there\n" +
  26. "Had worn them really about the same,\n" +
  27. "\n" +
  28. "And both that morning equally lay\n" +
  29. "In leaves no step had trodden black.\n" +
  30. "Oh, I kept the first for another day!\n" +
  31. "Yet knowing how way leads on to way,\n" +
  32. "I doubted if I should ever come back.\n" +
  33. "\n" +
  34. "I shall be telling this with a sigh\n" +
  35. "Somewhere ages and ages hence:\n" +
  36. "Two roads diverged in a wood, and I-\n" +
  37. "I took the one less traveled by,\n" +
  38. "And that has made all the difference.\n"
  39. )
  40. var poemRefs = []struct {
  41. dist int // Backward distance (0 if this is an insertion)
  42. length int // Length of copy or insertion
  43. }{
  44. {0, 38}, {33, 3}, {0, 48}, {79, 3}, {0, 11}, {34, 5}, {0, 6}, {23, 7},
  45. {0, 8}, {50, 3}, {0, 2}, {69, 3}, {34, 5}, {0, 4}, {97, 3}, {0, 4},
  46. {43, 5}, {0, 6}, {7, 4}, {88, 7}, {0, 12}, {80, 3}, {0, 2}, {141, 4},
  47. {0, 1}, {196, 3}, {0, 3}, {157, 3}, {0, 6}, {181, 3}, {0, 2}, {23, 3},
  48. {77, 3}, {28, 5}, {128, 3}, {110, 4}, {70, 3}, {0, 4}, {85, 6}, {0, 2},
  49. {182, 6}, {0, 4}, {133, 3}, {0, 7}, {47, 5}, {0, 20}, {112, 5}, {0, 1},
  50. {58, 3}, {0, 8}, {59, 3}, {0, 4}, {173, 3}, {0, 5}, {114, 3}, {0, 4},
  51. {92, 5}, {0, 2}, {71, 3}, {0, 2}, {76, 5}, {0, 1}, {46, 3}, {96, 4},
  52. {130, 4}, {0, 3}, {360, 3}, {0, 3}, {178, 5}, {0, 7}, {75, 3}, {0, 3},
  53. {45, 6}, {0, 6}, {299, 6}, {180, 3}, {70, 6}, {0, 1}, {48, 3}, {66, 4},
  54. {0, 3}, {47, 5}, {0, 9}, {325, 3}, {0, 1}, {359, 3}, {318, 3}, {0, 2},
  55. {199, 3}, {0, 1}, {344, 3}, {0, 3}, {248, 3}, {0, 10}, {310, 3}, {0, 3},
  56. {93, 6}, {0, 3}, {252, 3}, {157, 4}, {0, 2}, {273, 5}, {0, 14}, {99, 4},
  57. {0, 1}, {464, 4}, {0, 2}, {92, 4}, {495, 3}, {0, 1}, {322, 4}, {16, 4},
  58. {0, 3}, {402, 3}, {0, 2}, {237, 4}, {0, 2}, {432, 4}, {0, 1}, {483, 5},
  59. {0, 2}, {294, 4}, {0, 2}, {306, 3}, {113, 5}, {0, 1}, {26, 4}, {164, 3},
  60. {488, 4}, {0, 1}, {542, 3}, {248, 6}, {0, 5}, {205, 3}, {0, 8}, {48, 3},
  61. {449, 6}, {0, 2}, {192, 3}, {328, 4}, {9, 5}, {433, 3}, {0, 3}, {622, 25},
  62. {615, 5}, {46, 5}, {0, 2}, {104, 3}, {475, 10}, {549, 3}, {0, 4}, {597, 8},
  63. {314, 3}, {0, 1}, {473, 6}, {317, 5}, {0, 1}, {400, 3}, {0, 3}, {109, 3},
  64. {151, 3}, {48, 4}, {0, 4}, {125, 3}, {108, 3}, {0, 2},
  65. }
  66. var got, want bytes.Buffer
  67. var dd dictDecoder
  68. dd.init(1<<11, nil)
  69. var writeCopy = func(dist, length int) {
  70. for length > 0 {
  71. cnt := dd.tryWriteCopy(dist, length)
  72. if cnt == 0 {
  73. cnt = dd.writeCopy(dist, length)
  74. }
  75. length -= cnt
  76. if dd.availWrite() == 0 {
  77. got.Write(dd.readFlush())
  78. }
  79. }
  80. }
  81. var writeString = func(str string) {
  82. for len(str) > 0 {
  83. cnt := copy(dd.writeSlice(), str)
  84. str = str[cnt:]
  85. dd.writeMark(cnt)
  86. if dd.availWrite() == 0 {
  87. got.Write(dd.readFlush())
  88. }
  89. }
  90. }
  91. writeString(".")
  92. want.WriteByte('.')
  93. str := poem
  94. for _, ref := range poemRefs {
  95. if ref.dist == 0 {
  96. writeString(str[:ref.length])
  97. } else {
  98. writeCopy(ref.dist, ref.length)
  99. }
  100. str = str[ref.length:]
  101. }
  102. want.WriteString(poem)
  103. writeCopy(dd.histSize(), 33)
  104. want.Write(want.Bytes()[:33])
  105. writeString(abc)
  106. writeCopy(len(abc), 59*len(abc))
  107. want.WriteString(strings.Repeat(abc, 60))
  108. writeString(fox)
  109. writeCopy(len(fox), 9*len(fox))
  110. want.WriteString(strings.Repeat(fox, 10))
  111. writeString(".")
  112. writeCopy(1, 9)
  113. want.WriteString(strings.Repeat(".", 10))
  114. writeString(strings.ToUpper(poem))
  115. writeCopy(len(poem), 7*len(poem))
  116. want.WriteString(strings.Repeat(strings.ToUpper(poem), 8))
  117. writeCopy(dd.histSize(), 10)
  118. want.Write(want.Bytes()[want.Len()-dd.histSize():][:10])
  119. got.Write(dd.readFlush())
  120. if got.String() != want.String() {
  121. t.Errorf("final string mismatch:\ngot %q\nwant %q", got.String(), want.String())
  122. }
  123. }