1 // Copyright 2011 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.
14 // equal returns nil if p and q describe the same file set;
15 // otherwise it returns an error describing the discrepancy.
16 func equal(p, q *FileSet) error {
18 // avoid deadlock if p == q
22 // not strictly needed for the test
25 defer q.mutex.Unlock()
26 defer p.mutex.Unlock()
29 return fmt.Errorf("different bases: %d != %d", p.base, q.base)
32 if len(p.files) != len(q.files) {
33 return fmt.Errorf("different number of files: %d != %d", len(p.files), len(q.files))
36 for i, f := range p.files {
39 return fmt.Errorf("wrong fileset for %q", f.name)
42 return fmt.Errorf("wrong fileset for %q", g.name)
45 return fmt.Errorf("different filenames: %q != %q", f.name, g.name)
48 return fmt.Errorf("different base for %q: %d != %d", f.name, f.base, g.base)
51 return fmt.Errorf("different size for %q: %d != %d", f.name, f.size, g.size)
53 for j, l := range f.lines {
56 return fmt.Errorf("different offsets for %q", f.name)
59 for j, l := range f.infos {
61 if l.Offset != m.Offset || l.Filename != m.Filename || l.Line != m.Line {
62 return fmt.Errorf("different infos for %q", f.name)
67 // we don't care about .last - it's just a cache
71 func checkSerialize(t *testing.T, p *FileSet) {
73 encode := func(x interface{}) error {
74 return gob.NewEncoder(&buf).Encode(x)
76 if err := p.Write(encode); err != nil {
77 t.Errorf("writing fileset failed: %s", err)
81 decode := func(x interface{}) error {
82 return gob.NewDecoder(&buf).Decode(x)
84 if err := q.Read(decode); err != nil {
85 t.Errorf("reading fileset failed: %s", err)
88 if err := equal(p, q); err != nil {
89 t.Errorf("filesets not identical: %s", err)
93 func TestSerialization(t *testing.T) {
97 for i := 0; i < 10; i++ {
98 f := p.AddFile(fmt.Sprintf("file%d", i), p.Base()+i, i*100)
100 // add some lines and alternative file infos
102 for offs := 0; offs < f.Size(); offs += 40 + i {
105 f.AddLineInfo(offs, fmt.Sprintf("file%d", offs), line)