Lorenz Brun | 378a445 | 2021-01-26 13:47:41 +0100 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | package erofs |
| 18 | |
| 19 | import ( |
| 20 | "reflect" |
| 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | func TestEncodeSmallVLEBlock(t *testing.T) { |
| 25 | type args struct { |
| 26 | vals [2]uint16 |
| 27 | blkaddr uint32 |
| 28 | } |
| 29 | tests := []struct { |
| 30 | name string |
| 31 | args args |
| 32 | want [8]byte |
| 33 | }{ |
| 34 | { |
| 35 | name: "Reference", |
| 36 | args: args{vals: [2]uint16{vleClusterTypeHead | 1527, vleClusterTypeNonhead | 1}, blkaddr: 1}, |
| 37 | want: [8]byte{0xf7, 0x15, 0x01, 0x20, 0x01, 0x00, 0x00, 0x00}, |
| 38 | }, |
| 39 | } |
| 40 | for _, tt := range tests { |
| 41 | t.Run(tt.name, func(t *testing.T) { |
| 42 | if got := encodeSmallVLEBlock(tt.args.vals, tt.args.blkaddr); !reflect.DeepEqual(got, tt.want) { |
| 43 | t.Errorf("encodeSmallVLEBlock() = %v, want %v", got, tt.want) |
| 44 | } |
| 45 | }) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestEncodeBigVLEBlock(t *testing.T) { |
| 50 | type args struct { |
| 51 | vals [16]uint16 |
| 52 | blkaddr uint32 |
| 53 | } |
| 54 | tests := []struct { |
| 55 | name string |
| 56 | args args |
| 57 | want [32]byte |
| 58 | }{ |
| 59 | { |
| 60 | name: "Reference", |
| 61 | args: args{ |
| 62 | vals: [16]uint16{ |
| 63 | vleClusterTypeNonhead | 2, |
| 64 | vleClusterTypeHead | 1460, |
| 65 | vleClusterTypeNonhead | 1, |
| 66 | vleClusterTypeNonhead | 2, |
| 67 | vleClusterTypeHead | 2751, |
| 68 | vleClusterTypeNonhead | 1, |
| 69 | vleClusterTypeNonhead | 2, |
| 70 | vleClusterTypeHead | 940, |
| 71 | vleClusterTypeNonhead | 1, |
| 72 | vleClusterTypeHead | 3142, |
| 73 | vleClusterTypeNonhead | 1, |
| 74 | vleClusterTypeNonhead | 2, |
| 75 | vleClusterTypeHead | 1750, |
| 76 | vleClusterTypeNonhead | 1, |
| 77 | vleClusterTypeNonhead | 2, |
| 78 | vleClusterTypeHead | 683, |
| 79 | }, |
| 80 | blkaddr: 3, |
| 81 | }, |
| 82 | want: [32]byte{0x02, 0x20, 0x6d, 0x15, 0x00, 0x0a, 0x80, 0xbf, 0x5a, 0x00, 0x28, 0x00, 0xb2, 0x4e, 0x01, 0xa0, 0x11, 0x17, 0x00, 0x0a, 0x80, 0xd6, 0x56, 0x00, 0x28, 0x00, 0xae, 0x4a, 0x03, 0x00, 0x00, 0x00}}, |
| 83 | } |
| 84 | for _, tt := range tests { |
| 85 | t.Run(tt.name, func(t *testing.T) { |
| 86 | if got := encodeBigVLEBlock(tt.args.vals, tt.args.blkaddr); !reflect.DeepEqual(got, tt.want) { |
| 87 | t.Errorf("encodeBigVLEBlock() = %v, want %v", got, tt.want) |
| 88 | } |
| 89 | }) |
| 90 | } |
| 91 | } |