| // Copyright (C) 2014 The Android Open Source Project |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package test |
| |
| import ( |
| "bytes" |
| "reflect" |
| |
| "io" |
| |
| "android.googlesource.com/platform/tools/gpu/binary" |
| "android.googlesource.com/platform/tools/gpu/log" |
| ) |
| |
| type ReadWriteTests struct { |
| Name string |
| Values interface{} |
| Data []byte |
| } |
| |
| type Factory func(io.Reader, io.Writer) (binary.Reader, binary.Writer) |
| |
| func ReadWrite(ctx log.Context, tests []ReadWriteTests, factory Factory) { |
| for _, e := range tests { |
| ctx := ctx.S("name", e.Name) |
| b := &bytes.Buffer{} |
| reader, writer := factory(b, b) |
| r := reflect.ValueOf(reader).MethodByName(e.Name) |
| w := reflect.ValueOf(writer).MethodByName(e.Name) |
| s := reflect.ValueOf(e.Values) |
| for i := 0; i < s.Len(); i++ { |
| w.Call([]reflect.Value{s.Index(i)}) |
| } |
| ctx.Expect(e.Data).Got(b.Bytes()).Log("unexpected bytes") |
| for i := 0; i < s.Len(); i++ { |
| ctx := ctx.I("index", i) |
| expected := s.Index(i) |
| result := r.Call(nil) |
| got := result[0] |
| if reader.Error() != nil { |
| ctx.Fail(reader.Error(), "unexpected error") |
| } |
| ctx.Expect(expected.Interface()).Got(got.Interface()).Log("unexpected value") |
| } |
| } |
| } |
| |
| func ReadWriteData(ctx log.Context, tests []ReadWriteTests, factory Factory) { |
| for _, e := range tests { |
| ctx := ctx.S("name", e.Name) |
| b := &bytes.Buffer{} |
| reader, writer := factory(b, b) |
| writer.Data(e.Data) |
| ctx.Expect(e.Data).Got(b.Bytes()).Log("unexpected bytes") |
| got := make([]byte, len(e.Data)) |
| reader.Data(got) |
| ctx.Expect(e.Data).Got(got).Log("unexpected value") |
| } |
| } |
| |
| func ReadWriteCount(ctx log.Context, values []uint32, raw []byte, factory Factory) { |
| b := &bytes.Buffer{} |
| reader, writer := factory(b, b) |
| for _, v := range values { |
| writer.Uint32(v) |
| } |
| ctx.Expect(raw).Got(b.Bytes()).Log("unexpected bytes") |
| for _, expect := range values { |
| got := reader.Count() |
| ctx.Expect(expect).Got(got).Log("count value") |
| } |
| } |
| |
| func ReadWriteSimple(ctx log.Context, values []Simple, raw []byte, factory Factory) { |
| b := &bytes.Buffer{} |
| reader, writer := factory(b, b) |
| for _, v := range values { |
| writer.Simple(v) |
| } |
| ctx.Expect(raw).Got(b.Bytes()).Log("unexpected bytes") |
| for _, expect := range values { |
| var got Simple |
| reader.Simple(&got) |
| ctx.Expect(expect).Got(got).Log("simple value") |
| } |
| } |
| |
| func ReadWriteErrors(ctx log.Context, tests []ReadWriteTests, factory Factory) { |
| for _, e := range tests { |
| ctx := ctx.S("name", e.Name) |
| b := &bytes.Buffer{} |
| reader, writer := factory(b, b) |
| r := reflect.ValueOf(reader).MethodByName(e.Name) |
| w := reflect.ValueOf(writer).MethodByName(e.Name) |
| s := reflect.ValueOf(e.Values) |
| writer.SetError(WriteError) |
| w.Call([]reflect.Value{s.Index(0)}) |
| ctx.Expect(WriteError).Got(writer.Error()).Log("First Write") |
| writer.SetError(SecondError) |
| w.Call([]reflect.Value{s.Index(0)}) |
| ctx.Expect("Error:Failed:Compound error<SecondError>").Got(writer.Error().Error()).Log("Second Write") |
| reader.SetError(ReadError) |
| r.Call(nil) |
| ctx.Expect(ReadError).Got(reader.Error()).Log("First Read") |
| reader.SetError(SecondError) |
| r.Call(nil) |
| ctx.Expect("Error:Failed:Compound error<SecondError>").Got(reader.Error().Error()).Log("Second Read") |
| } |
| b := &bytes.Buffer{} |
| data := []byte{1} |
| reader, writer := factory(b, b) |
| writer.SetError(WriteError) |
| writer.Data(data) |
| ctx.Expect(WriteError).Got(writer.Error()).Log("Data Write") |
| reader.SetError(ReadError) |
| reader.Data(data) |
| ctx.Expect(ReadError).Got(reader.Error()).Log("Data Read") |
| } |
| |
| func ReadWriteIOErrors(ctx log.Context, tests []ReadWriteTests, factory Factory) { |
| for _, e := range tests { |
| ctx := ctx.S("name", e.Name) |
| reader, writer := factory(&Bytes{}, &LimitedWriter{}) |
| r := reflect.ValueOf(reader).MethodByName(e.Name) |
| w := reflect.ValueOf(writer).MethodByName(e.Name) |
| s := reflect.ValueOf(e.Values) |
| // Write twice, first time errors, second time compounds the error |
| w.Call([]reflect.Value{s.Index(0)}) |
| ctx.Expect(WriteError).Got(writer.Error()).Log("Bad write") |
| // Read twice, first time errors, second time compounds the error |
| r.Call(nil) |
| ctx.Expect(ReadError).Got(reader.Error()).Log("Bad read") |
| } |
| buf := []byte{1} |
| data := []byte{1, 2} |
| reader, writer := factory(&Bytes{Data: buf}, &LimitedWriter{Limit: 1}) |
| writer.Data(data) |
| ctx.Expect(io.ErrShortWrite).Got(writer.Error()).Log("Data Write") |
| reader.Data(data) |
| ctx.Expect(ReadError).Got(reader.Error()).Log("Data Read") |
| } |