|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package storage |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | +) |
| 21 | + |
| 22 | +func TestAddFeatureAttributes(t *testing.T) { |
| 23 | + ctx := context.Background() |
| 24 | + |
| 25 | + // Initial features should be 0. |
| 26 | + if got := featureAttributes(ctx); got != 0 { |
| 27 | + t.Errorf("getFeatureAttributes(empty) = %d; want 0", got) |
| 28 | + } |
| 29 | + |
| 30 | + // Add a single feature. |
| 31 | + ctx = addFeatureAttributes(ctx, featureMultistreamInMRD) |
| 32 | + if got := featureAttributes(ctx); got != uint32(1<<featureMultistreamInMRD) { |
| 33 | + t.Errorf("getFeatureAttributes(MultiStream) = %d; want %d", got, featureMultistreamInMRD) |
| 34 | + } |
| 35 | + |
| 36 | + // Add another feature (merge). |
| 37 | + ctx = addFeatureAttributes(ctx, featurePCU) |
| 38 | + want := uint32(1<<featureMultistreamInMRD) | uint32(1<<featurePCU) |
| 39 | + if got := featureAttributes(ctx); got != want { |
| 40 | + t.Errorf("getFeatureAttributes(MultiStream | PCU) = %d; want %d", got, want) |
| 41 | + } |
| 42 | + |
| 43 | + // Adding same feature should be idempotent. |
| 44 | + ctx = addFeatureAttributes(ctx, featurePCU) |
| 45 | + if got := featureAttributes(ctx); got != want { |
| 46 | + t.Errorf("getFeatureAttributes(MultiStream | PCU | PCU) = %d; want %d", got, want) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestMergeFeatureAttributes(t *testing.T) { |
| 51 | + tests := []struct { |
| 52 | + name string |
| 53 | + vals []string |
| 54 | + want uint32 |
| 55 | + }{ |
| 56 | + { |
| 57 | + name: "empty", |
| 58 | + vals: []string{}, |
| 59 | + want: 0, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "single value", |
| 63 | + vals: []string{encodeUint32(1)}, |
| 64 | + want: 1, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "multiple values", |
| 68 | + vals: []string{encodeUint32(1), encodeUint32(2), encodeUint32(4)}, |
| 69 | + want: 7, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "overlapping values", |
| 73 | + vals: []string{encodeUint32(3), encodeUint32(6)}, |
| 74 | + want: 7, // 011 | 110 = 111 (7) |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "invalid values ignored", |
| 78 | + vals: []string{encodeUint32(1), "invalid", encodeUint32(8)}, |
| 79 | + want: 9, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, tc := range tests { |
| 84 | + t.Run(tc.name, func(t *testing.T) { |
| 85 | + got := mergeFeatureAttributes(tc.vals) |
| 86 | + if got != tc.want { |
| 87 | + t.Errorf("mergeFeatureAttributes(%v) = %d; want %d", tc.vals, got, tc.want) |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
0 commit comments