• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2017, 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.md file.
4
5package teststructs
6
7import (
8	"time"
9
10	pb "github.com/google/go-cmp/cmp/internal/testprotos"
11)
12
13// This is an sanitized example of equality from a real use-case.
14// The original equality function was as follows:
15/*
16func equalBatch(b1, b2 *GermBatch) bool {
17	for _, b := range []*GermBatch{b1, b2} {
18		for _, l := range b.DirtyGerms {
19			sort.Slice(l, func(i, j int) bool { return l[i].String() < l[j].String() })
20		}
21		for _, l := range b.CleanGerms {
22			sort.Slice(l, func(i, j int) bool { return l[i].String() < l[j].String() })
23		}
24	}
25	if !pb.DeepEqual(b1.DirtyGerms, b2.DirtyGerms) ||
26		!pb.DeepEqual(b1.CleanGerms, b2.CleanGerms) ||
27		!pb.DeepEqual(b1.GermMap, b2.GermMap) {
28		return false
29	}
30	if len(b1.DishMap) != len(b2.DishMap) {
31		return false
32	}
33	for id := range b1.DishMap {
34		kpb1, err1 := b1.DishMap[id].Proto()
35		kpb2, err2 := b2.DishMap[id].Proto()
36		if !pb.Equal(kpb1, kpb2) || !reflect.DeepEqual(err1, err2) {
37			return false
38		}
39	}
40	return b1.HasPreviousResult == b2.HasPreviousResult &&
41		b1.DirtyID == b2.DirtyID &&
42		b1.CleanID == b2.CleanID &&
43		b1.GermStrain == b2.GermStrain &&
44		b1.TotalDirtyGerms == b2.TotalDirtyGerms &&
45		b1.InfectedAt.Equal(b2.InfectedAt)
46}
47*/
48
49type GermBatch struct {
50	DirtyGerms, CleanGerms map[int32][]*pb.Germ
51	GermMap                map[int32]*pb.Germ
52	DishMap                map[int32]*Dish
53	HasPreviousResult      bool
54	DirtyID, CleanID       int32
55	GermStrain             int32
56	TotalDirtyGerms        int
57	InfectedAt             time.Time
58}
59
60type Dish struct {
61	pb  *pb.Dish
62	err error
63}
64
65func CreateDish(m *pb.Dish, err error) *Dish {
66	return &Dish{pb: m, err: err}
67}
68
69func (d *Dish) Proto() (*pb.Dish, error) {
70	if d.err != nil {
71		return nil, d.err
72	}
73	return d.pb, nil
74}
75