~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/hash/adler32/adler32_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
package adler32
6
6
 
7
7
import (
8
 
        "bytes"
9
 
        "io"
 
8
        "strings"
10
9
        "testing"
11
10
)
12
11
 
13
 
type _Adler32Test struct {
 
12
var golden = []struct {
14
13
        out uint32
15
14
        in  string
16
 
}
17
 
 
18
 
var golden = []_Adler32Test{
19
 
        {0x1, ""},
20
 
        {0x620062, "a"},
21
 
        {0x12600c4, "ab"},
22
 
        {0x24d0127, "abc"},
23
 
        {0x3d8018b, "abcd"},
24
 
        {0x5c801f0, "abcde"},
25
 
        {0x81e0256, "abcdef"},
26
 
        {0xadb02bd, "abcdefg"},
27
 
        {0xe000325, "abcdefgh"},
 
15
}{
 
16
        {0x00000001, ""},
 
17
        {0x00620062, "a"},
 
18
        {0x012600c4, "ab"},
 
19
        {0x024d0127, "abc"},
 
20
        {0x03d8018b, "abcd"},
 
21
        {0x05c801f0, "abcde"},
 
22
        {0x081e0256, "abcdef"},
 
23
        {0x0adb02bd, "abcdefg"},
 
24
        {0x0e000325, "abcdefgh"},
28
25
        {0x118e038e, "abcdefghi"},
29
26
        {0x158603f8, "abcdefghij"},
30
27
        {0x3f090f02, "Discard medicine more than two years old."},
48
45
        {0x91dd304f, "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction.  Lewis-Randall Rule"},
49
46
        {0x2e5d1316, "How can you write a big system without C++?  -Paul Glick"},
50
47
        {0xd0201df6, "'Invariant assertions' is the most elegant programming technique!  -Tom Szymanski"},
 
48
        {0x211297c8, strings.Repeat("\xff", 5548) + "8"},
 
49
        {0xbaa198c8, strings.Repeat("\xff", 5549) + "9"},
 
50
        {0x553499be, strings.Repeat("\xff", 5550) + "0"},
 
51
        {0xf0c19abe, strings.Repeat("\xff", 5551) + "1"},
 
52
        {0x8d5c9bbe, strings.Repeat("\xff", 5552) + "2"},
 
53
        {0x2af69cbe, strings.Repeat("\xff", 5553) + "3"},
 
54
        {0xc9809dbe, strings.Repeat("\xff", 5554) + "4"},
 
55
        {0x69189ebe, strings.Repeat("\xff", 5555) + "5"},
 
56
        {0x86af0001, strings.Repeat("\x00", 1e5)},
 
57
        {0x79660b4d, strings.Repeat("a", 1e5)},
 
58
        {0x110588ee, strings.Repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1e4)},
 
59
}
 
60
 
 
61
// checksum is a slow but simple implementation of the Adler-32 checksum.
 
62
// It is a straight port of the sample code in RFC 1950 section 9.
 
63
func checksum(p []byte) uint32 {
 
64
        s1, s2 := uint32(1), uint32(0)
 
65
        for _, x := range p {
 
66
                s1 = (s1 + uint32(x)) % mod
 
67
                s2 = (s2 + s1) % mod
 
68
        }
 
69
        return s2<<16 | s1
51
70
}
52
71
 
53
72
func TestGolden(t *testing.T) {
54
 
        for i := 0; i < len(golden); i++ {
55
 
                g := golden[i]
56
 
                c := New()
57
 
                io.WriteString(c, g.in)
58
 
                s := c.Sum32()
59
 
                if s != g.out {
60
 
                        t.Errorf("adler32(%s) = 0x%x want 0x%x", g.in, s, g.out)
61
 
                        t.FailNow()
62
 
                }
63
 
        }
64
 
}
65
 
 
66
 
func BenchmarkGolden(b *testing.B) {
67
 
        b.StopTimer()
68
 
        c := New()
69
 
        var buf bytes.Buffer
70
73
        for _, g := range golden {
71
 
                buf.Write([]byte(g.in))
72
 
        }
73
 
        b.StartTimer()
 
74
                in := g.in
 
75
                if len(in) > 220 {
 
76
                        in = in[:100] + "..." + in[len(in)-100:]
 
77
                }
 
78
                p := []byte(g.in)
 
79
                if got := checksum(p); got != g.out {
 
80
                        t.Errorf("simple implementation: checksum(%q) = 0x%x want 0x%x", in, got, g.out)
 
81
                        continue
 
82
                }
 
83
                if got := Checksum(p); got != g.out {
 
84
                        t.Errorf("optimized implementation: Checksum(%q) = 0x%x want 0x%x", in, got, g.out)
 
85
                        continue
 
86
                }
 
87
        }
 
88
}
 
89
 
 
90
func BenchmarkAdler32KB(b *testing.B) {
 
91
        b.SetBytes(1024)
 
92
        data := make([]byte, 1024)
 
93
        for i := range data {
 
94
                data[i] = byte(i)
 
95
        }
 
96
        h := New()
 
97
        in := make([]byte, 0, h.Size())
 
98
 
 
99
        b.ResetTimer()
74
100
        for i := 0; i < b.N; i++ {
75
 
                c.Write(buf.Bytes())
 
101
                h.Reset()
 
102
                h.Write(data)
 
103
                h.Sum(in)
76
104
        }
77
105
}