github.com/gogo/protobuf@v1.3.2/test/issue330/issue330pb_test.go (about) 1 // Code generated by protoc-gen-gogo. DO NOT EDIT. 2 // source: issue330.proto 3 4 package issue330 5 6 import ( 7 fmt "fmt" 8 _ "github.com/gogo/protobuf/gogoproto" 9 github_com_gogo_protobuf_jsonpb "github.com/gogo/protobuf/jsonpb" 10 github_com_gogo_protobuf_proto "github.com/gogo/protobuf/proto" 11 proto "github.com/gogo/protobuf/proto" 12 math "math" 13 math_rand "math/rand" 14 testing "testing" 15 time "time" 16 ) 17 18 // Reference imports to suppress errors if they are not otherwise used. 19 var _ = proto.Marshal 20 var _ = fmt.Errorf 21 var _ = math.Inf 22 23 func TestObjectProto(t *testing.T) { 24 seed := time.Now().UnixNano() 25 popr := math_rand.New(math_rand.NewSource(seed)) 26 p := NewPopulatedObject(popr, false) 27 dAtA, err := github_com_gogo_protobuf_proto.Marshal(p) 28 if err != nil { 29 t.Fatalf("seed = %d, err = %v", seed, err) 30 } 31 msg := &Object{} 32 if err := github_com_gogo_protobuf_proto.Unmarshal(dAtA, msg); err != nil { 33 t.Fatalf("seed = %d, err = %v", seed, err) 34 } 35 littlefuzz := make([]byte, len(dAtA)) 36 copy(littlefuzz, dAtA) 37 for i := range dAtA { 38 dAtA[i] = byte(popr.Intn(256)) 39 } 40 if !p.Equal(msg) { 41 t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) 42 } 43 if len(littlefuzz) > 0 { 44 fuzzamount := 100 45 for i := 0; i < fuzzamount; i++ { 46 littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256)) 47 littlefuzz = append(littlefuzz, byte(popr.Intn(256))) 48 } 49 // shouldn't panic 50 _ = github_com_gogo_protobuf_proto.Unmarshal(littlefuzz, msg) 51 } 52 } 53 54 func TestObjectMarshalTo(t *testing.T) { 55 seed := time.Now().UnixNano() 56 popr := math_rand.New(math_rand.NewSource(seed)) 57 p := NewPopulatedObject(popr, false) 58 size := p.Size() 59 dAtA := make([]byte, size) 60 for i := range dAtA { 61 dAtA[i] = byte(popr.Intn(256)) 62 } 63 _, err := p.MarshalTo(dAtA) 64 if err != nil { 65 t.Fatalf("seed = %d, err = %v", seed, err) 66 } 67 msg := &Object{} 68 if err := github_com_gogo_protobuf_proto.Unmarshal(dAtA, msg); err != nil { 69 t.Fatalf("seed = %d, err = %v", seed, err) 70 } 71 for i := range dAtA { 72 dAtA[i] = byte(popr.Intn(256)) 73 } 74 if !p.Equal(msg) { 75 t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) 76 } 77 } 78 79 func BenchmarkObjectProtoMarshal(b *testing.B) { 80 popr := math_rand.New(math_rand.NewSource(616)) 81 total := 0 82 pops := make([]*Object, 10000) 83 for i := 0; i < 10000; i++ { 84 pops[i] = NewPopulatedObject(popr, false) 85 } 86 b.ResetTimer() 87 for i := 0; i < b.N; i++ { 88 dAtA, err := github_com_gogo_protobuf_proto.Marshal(pops[i%10000]) 89 if err != nil { 90 panic(err) 91 } 92 total += len(dAtA) 93 } 94 b.SetBytes(int64(total / b.N)) 95 } 96 97 func BenchmarkObjectProtoUnmarshal(b *testing.B) { 98 popr := math_rand.New(math_rand.NewSource(616)) 99 total := 0 100 datas := make([][]byte, 10000) 101 for i := 0; i < 10000; i++ { 102 dAtA, err := github_com_gogo_protobuf_proto.Marshal(NewPopulatedObject(popr, false)) 103 if err != nil { 104 panic(err) 105 } 106 datas[i] = dAtA 107 } 108 msg := &Object{} 109 b.ResetTimer() 110 for i := 0; i < b.N; i++ { 111 total += len(datas[i%10000]) 112 if err := github_com_gogo_protobuf_proto.Unmarshal(datas[i%10000], msg); err != nil { 113 panic(err) 114 } 115 } 116 b.SetBytes(int64(total / b.N)) 117 } 118 119 func TestObjectJSON(t *testing.T) { 120 seed := time.Now().UnixNano() 121 popr := math_rand.New(math_rand.NewSource(seed)) 122 p := NewPopulatedObject(popr, true) 123 marshaler := github_com_gogo_protobuf_jsonpb.Marshaler{} 124 jsondata, err := marshaler.MarshalToString(p) 125 if err != nil { 126 t.Fatalf("seed = %d, err = %v", seed, err) 127 } 128 msg := &Object{} 129 err = github_com_gogo_protobuf_jsonpb.UnmarshalString(jsondata, msg) 130 if err != nil { 131 t.Fatalf("seed = %d, err = %v", seed, err) 132 } 133 if !p.Equal(msg) { 134 t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p) 135 } 136 } 137 func TestObjectProtoText(t *testing.T) { 138 seed := time.Now().UnixNano() 139 popr := math_rand.New(math_rand.NewSource(seed)) 140 p := NewPopulatedObject(popr, true) 141 dAtA := github_com_gogo_protobuf_proto.MarshalTextString(p) 142 msg := &Object{} 143 if err := github_com_gogo_protobuf_proto.UnmarshalText(dAtA, msg); err != nil { 144 t.Fatalf("seed = %d, err = %v", seed, err) 145 } 146 if !p.Equal(msg) { 147 t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) 148 } 149 } 150 151 func TestObjectProtoCompactText(t *testing.T) { 152 seed := time.Now().UnixNano() 153 popr := math_rand.New(math_rand.NewSource(seed)) 154 p := NewPopulatedObject(popr, true) 155 dAtA := github_com_gogo_protobuf_proto.CompactTextString(p) 156 msg := &Object{} 157 if err := github_com_gogo_protobuf_proto.UnmarshalText(dAtA, msg); err != nil { 158 t.Fatalf("seed = %d, err = %v", seed, err) 159 } 160 if !p.Equal(msg) { 161 t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) 162 } 163 } 164 165 func TestObjectSize(t *testing.T) { 166 seed := time.Now().UnixNano() 167 popr := math_rand.New(math_rand.NewSource(seed)) 168 p := NewPopulatedObject(popr, true) 169 size2 := github_com_gogo_protobuf_proto.Size(p) 170 dAtA, err := github_com_gogo_protobuf_proto.Marshal(p) 171 if err != nil { 172 t.Fatalf("seed = %d, err = %v", seed, err) 173 } 174 size := p.Size() 175 if len(dAtA) != size { 176 t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA)) 177 } 178 if size2 != size { 179 t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2) 180 } 181 size3 := github_com_gogo_protobuf_proto.Size(p) 182 if size3 != size { 183 t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3) 184 } 185 } 186 187 func BenchmarkObjectSize(b *testing.B) { 188 popr := math_rand.New(math_rand.NewSource(616)) 189 total := 0 190 pops := make([]*Object, 1000) 191 for i := 0; i < 1000; i++ { 192 pops[i] = NewPopulatedObject(popr, false) 193 } 194 b.ResetTimer() 195 for i := 0; i < b.N; i++ { 196 total += pops[i%1000].Size() 197 } 198 b.SetBytes(int64(total / b.N)) 199 } 200 201 //These tests are generated by github.com/gogo/protobuf/plugin/testgen