Debian Codegraph @

src/main/java/io/airlift/slice/testing/SliceAssertions.java

raw
1 /*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14 package io.airlift.slice.testing;
15
16 import io.airlift.slice.Slice;
17
18 public class SliceAssertions
19 {
20 public static void assertSlicesEqual(Slice actual, Slice expected)
21 {
22 if (actual == null && expected == null) {
23 return;
24 }
25 else if (actual == null) {
26 throw new AssertionError("Actual is null");
27 }
28 else if (expected == null) {
29 throw new AssertionError("Expected actual to be null");
30 }
31
32 if (actual.length() != expected.length()) {
33 throw new AssertionError(String.format("Slices differ in size. Actual: %s, expected: %s", actual.length(), expected.length()));
34 }
35
36 for (int i = 0; i < actual.length(); i++) {
37 if (actual.getByte(i) != expected.getByte(i)) {
38 throw new AssertionError(String.format("Slices differ at index %s. Actual: 0x%02x, expected: 0x%02x", i, actual.getUnsignedByte(i), expected.getUnsignedByte(i)));
39 }
40 }
41 }
42
43 }