|
| 1 | +/* |
| 2 | + * TupleHelpersTest.java |
| 3 | + * |
| 4 | + * This source file is part of the FoundationDB open source project |
| 5 | + * |
| 6 | + * Copyright 2015-2025 Apple Inc. and the FoundationDB project authors |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.apple.foundationdb.tuple; |
| 22 | + |
| 23 | +import org.junit.jupiter.params.ParameterizedTest; |
| 24 | +import org.junit.jupiter.params.provider.Arguments; |
| 25 | +import org.junit.jupiter.params.provider.MethodSource; |
| 26 | + |
| 27 | +import java.util.UUID; |
| 28 | +import java.util.stream.Stream; |
| 29 | + |
| 30 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 32 | + |
| 33 | +class TupleHelpersTest { |
| 34 | + |
| 35 | + static Stream<Arguments> isPrefixTrueCases() { |
| 36 | + UUID uuid1 = UUID.fromString("12345678-1234-1234-1234-123456789abc"); |
| 37 | + byte[] data1 = {0x01, 0x02, 0x03}; |
| 38 | + byte[] data2 = {0x01, 0x02, 0x03}; |
| 39 | + |
| 40 | + return Stream.of( |
| 41 | + Arguments.of("empty tuples", |
| 42 | + Tuple.from(), |
| 43 | + Tuple.from()), |
| 44 | + Arguments.of("empty prefix and non-empty target", |
| 45 | + Tuple.from(), |
| 46 | + Tuple.from("test", 42)), |
| 47 | + Arguments.of("single element match", |
| 48 | + Tuple.from("test"), |
| 49 | + Tuple.from("test", 42, true)), |
| 50 | + Arguments.of("multiple elements match", |
| 51 | + Tuple.from("test", 42, true), |
| 52 | + Tuple.from("test", 42, true, "more", "data")), |
| 53 | + Arguments.of("equal tuples", |
| 54 | + Tuple.from("test", 42, true), |
| 55 | + Tuple.from("test", 42, true)), |
| 56 | + Arguments.of("different data types", |
| 57 | + Tuple.from("string", 123L, 4.5f, 6.7d, true), |
| 58 | + Tuple.from("string", 123L, 4.5f, 6.7d, true, "more", false)), |
| 59 | + Arguments.of("mixed type match", |
| 60 | + Tuple.from("string", 123), |
| 61 | + Tuple.from("string", 123L)), |
| 62 | + Arguments.of("binary data", |
| 63 | + Tuple.from("test", data1), |
| 64 | + Tuple.from("test", data2, "more")), |
| 65 | + Arguments.of("uuids", |
| 66 | + Tuple.from("test", uuid1), |
| 67 | + Tuple.from("test", uuid1, UUID.randomUUID())), |
| 68 | + Arguments.of("null values", |
| 69 | + Tuple.from("test", null), |
| 70 | + Tuple.from("test", null, "more")), |
| 71 | + Arguments.of("number variations", |
| 72 | + Tuple.from(42, 3.14, -7L), |
| 73 | + Tuple.from(42, 3.14, -7L, "suffix")), |
| 74 | + Arguments.of("complex data", |
| 75 | + Tuple.from("company", 1L, "engineering", 100L), |
| 76 | + Tuple.from("company", 1L, "engineering", 100L, "employee_data", true, 4.5f)), |
| 77 | + Arguments.of("nested tuple match", |
| 78 | + Tuple.from("outer", Tuple.from("inner", 42)), |
| 79 | + Tuple.from("outer", Tuple.from("inner", 42), "more")), |
| 80 | + Arguments.of("nested tuple with different depths", |
| 81 | + Tuple.from("root", Tuple.from("level1", Tuple.from("level2", "value"))), |
| 82 | + Tuple.from("root", Tuple.from("level1", Tuple.from("level2", "value")), "extra", "data")), |
| 83 | + Arguments.of("empty nested tuple", |
| 84 | + Tuple.from("outer", Tuple.from()), |
| 85 | + Tuple.from("outer", Tuple.from(), "suffix")) |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + @ParameterizedTest(name = "{0}") |
| 90 | + @MethodSource("isPrefixTrueCases") |
| 91 | + void isPrefixShouldReturnTrue(String description, Tuple prefix, Tuple target) { |
| 92 | + assertTrue(TupleHelpers.isPrefix(prefix, target)); |
| 93 | + } |
| 94 | + |
| 95 | + static Stream<Arguments> isPrefixFalseCases() { |
| 96 | + byte[] data1 = {0x01, 0x02, 0x03}; |
| 97 | + byte[] data2 = {0x01, 0x02, 0x04}; |
| 98 | + byte[] data3 = {0x01, 0x02}; |
| 99 | + |
| 100 | + return Stream.of( |
| 101 | + Arguments.of("non-empty prefix and empty target", |
| 102 | + Tuple.from("test"), |
| 103 | + Tuple.from()), |
| 104 | + Arguments.of("single element mismatch", |
| 105 | + Tuple.from("test"), |
| 106 | + Tuple.from("different", 42)), |
| 107 | + Arguments.of("multiple elements partial match", |
| 108 | + Tuple.from("test", 42, false), |
| 109 | + Tuple.from("test", 42, true, "more")), |
| 110 | + Arguments.of("prefix longer than target", |
| 111 | + Tuple.from("test", 42, true, "extra"), |
| 112 | + Tuple.from("test", 42)), |
| 113 | + Arguments.of("different binary data", |
| 114 | + Tuple.from("test", data1), |
| 115 | + Tuple.from("test", data2, "more")), |
| 116 | + Arguments.of("null mismatch", |
| 117 | + Tuple.from("test", null), |
| 118 | + Tuple.from("test", "not-null")), |
| 119 | + Arguments.of("number mismatch", |
| 120 | + Tuple.from(42, 3.14), |
| 121 | + Tuple.from(42, 3.15, "suffix")), |
| 122 | + Arguments.of("byte[] prefix of other", |
| 123 | + Tuple.from().add(data3), |
| 124 | + Tuple.from().add(data2)), |
| 125 | + Arguments.of("string prefix of other", |
| 126 | + Tuple.from().add("ap"), |
| 127 | + Tuple.from().add("apple")), |
| 128 | + Arguments.of("nested tuple mismatch", |
| 129 | + Tuple.from("outer", Tuple.from("inner", 42)), |
| 130 | + Tuple.from("outer", Tuple.from("inner", 43), "more")), |
| 131 | + Arguments.of("nested tuple different structure", |
| 132 | + Tuple.from("root", Tuple.from("level1", "value")), |
| 133 | + Tuple.from("root", Tuple.from("level2", "value"), "extra")), |
| 134 | + Arguments.of("nested vs non-nested", |
| 135 | + Tuple.from("outer", Tuple.from("inner")), |
| 136 | + Tuple.from("outer", "inner", "more")) |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + @ParameterizedTest(name = "{0}") |
| 141 | + @MethodSource("isPrefixFalseCases") |
| 142 | + void isPrefixShouldReturnFalse(String description, Tuple prefix, Tuple target) { |
| 143 | + assertFalse(TupleHelpers.isPrefix(prefix, target)); |
| 144 | + } |
| 145 | +} |
0 commit comments