| // Copyright (C) 2016 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 semantic_test |
| |
| import ( |
| "testing" |
| |
| "android.googlesource.com/platform/tools/gpu/api/semantic" |
| "android.googlesource.com/platform/tools/gpu/framework/assert" |
| ) |
| |
| type replaceTest struct { |
| name string |
| from int |
| count int |
| with semantic.Statements |
| expected semantic.Statements |
| } |
| |
| func TestStatementsReplace(t *testing.T) { |
| ctx := assert.Context(t) |
| |
| L := func(x ...semantic.Node) semantic.Statements { |
| return semantic.Statements(x) |
| } |
| |
| tests := []replaceTest{ |
| {"none", 4, 0, L(), L(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)}, |
| {"single", 4, 1, L(99), L(0, 1, 2, 3, 99, 5, 6, 7, 8, 9)}, |
| {"remove", 4, 1, L(), L(0, 1, 2, 3, 5, 6, 7, 8, 9)}, |
| {"insert", 4, 0, L(99), L(0, 1, 2, 3, 99, 4, 5, 6, 7, 8, 9)}, |
| {"many", 4, 2, L(70, 80, 90), L(0, 1, 2, 3, 70, 80, 90, 6, 7, 8, 9)}, |
| } |
| |
| ctx.Print("With cap == len") |
| for _, test := range tests { |
| ctx := ctx.S("test", test.name) |
| s := semantic.Statements{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} |
| s.Replace(test.from, test.count, test.with...) |
| assert.For(ctx, "statements").That(s).DeepEquals(test.expected) |
| } |
| |
| ctx.Print("With cap > len") |
| for _, test := range tests { |
| ctx := ctx.S("test", test.name) |
| s := make(semantic.Statements, 50)[:10] |
| copy(s, semantic.Statements{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) |
| s.Replace(test.from, test.count, test.with...) |
| assert.For(ctx, "statements").That(s).DeepEquals(test.expected) |
| } |
| } |
| |
| func TestStatementsRemove(t *testing.T) { |
| ctx := assert.Context(t) |
| |
| L := func(x ...semantic.Node) semantic.Statements { |
| return semantic.Statements(x) |
| } |
| |
| for _, test := range []struct { |
| n semantic.Node |
| expected semantic.Statements |
| }{ |
| {-1, L(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)}, |
| {5, L(0, 1, 2, 3, 4, 6, 7, 8, 9)}, |
| {9, L(0, 1, 2, 3, 4, 5, 6, 7, 8)}, |
| } { |
| ctx := ctx.V("removed", test.n) |
| s := semantic.Statements{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} |
| s.Remove(test.n) |
| assert.For(ctx, "statements").That(s).DeepEquals(test.expected) |
| } |
| } |