|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.plc4x.java.spi.generation; |
| 20 | + |
| 21 | +import org.apache.commons.codec.binary.Hex; |
| 22 | +import org.apache.plc4x.java.spi.codegen.WithOption; |
| 23 | +import org.junit.jupiter.params.ParameterizedTest; |
| 24 | +import org.junit.jupiter.params.provider.CsvSource; |
| 25 | + |
| 26 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 28 | + |
| 29 | +public class VarIntTest { |
| 30 | + |
| 31 | + @ParameterizedTest |
| 32 | + @CsvSource({ |
| 33 | + "'00', 0, 'Single‑byte encoding for 0'", |
| 34 | + "'01', 1, 'Single‑byte encoding for 1'", |
| 35 | + "'7F', 127, 'Single‑byte maximum (all 7 data bits set)'", |
| 36 | + "'8100', 128, 'Two‑byte encoding for 128; groups: [1, 0] (first byte 0x81, second byte 0x00)'", |
| 37 | + "'8148', 200, 'Two‑byte encoding for 200; groups: [1, 72] (0x81, 0x48)'", |
| 38 | + "'8600', 768, 'Two‑byte encoding for 768; groups: [6, 0] (0x86, 0x00) – as you indicated'", |
| 39 | + "'CE10', 10000, 'Two‑byte encoding for 10000; groups: [78, 16] (first byte: 78→0xCE, second: 16→0x10)'", |
| 40 | + "'FF7F', 16383, 'Two‑byte maximum (groups: [127, 127]; first byte 0xFF, second byte 0x7F)'", |
| 41 | + "'818000', 16384, 'Three‑byte encoding; smallest number needing three bytes; groups: [1, 0, 0]'", |
| 42 | + "'FFFF7F', 2097151, 'Three‑byte maximum; groups: [127, 127, 127] (encoded as 0xFF, 0xFF, 0x7F)'", |
| 43 | + "'82B19640', 5000000, 'Four‑byte encoding for 5000000; groups: [2, 49, 22, 64] (0x82, 0xB1, 0x96, 0x40)'", |
| 44 | + "'FFFFFF7F', 268435455, 'Four‑byte maximum; groups: [127, 127, 127, 127] (encoded as 0xFF, 0xFF, 0xFF, 0x7F)'", |
| 45 | + }) |
| 46 | + void writeVarUintRoundtrip(String hexString, long expectedValue, String description) throws Exception { |
| 47 | + byte[] serialized = Hex.decodeHex(hexString); |
| 48 | + |
| 49 | + // Parse the given array into a value |
| 50 | + ReadBufferByteBased readBuffer = new ReadBufferByteBased(serialized); |
| 51 | + long value = readBuffer.readUnsignedLong("", 32, WithOption.WithEncoding("VARUDINT")); |
| 52 | + assertEquals(expectedValue, value); |
| 53 | + |
| 54 | + // Serialize the given value into a byte array |
| 55 | + WriteBufferByteBased writeBuffer = new WriteBufferByteBased(serialized.length); |
| 56 | + writeBuffer.writeUnsignedLong("", 32, expectedValue, WithOption.WithEncoding("VARUDINT")); |
| 57 | + byte[] result = writeBuffer.getBytes(); |
| 58 | + assertArrayEquals(serialized, result, description); |
| 59 | + } |
| 60 | + |
| 61 | + @ParameterizedTest |
| 62 | + @CsvSource({ |
| 63 | + "'00', 0, '0 encoded in one byte.'", |
| 64 | + "'01', 1, 'Minimal positive value.'", |
| 65 | + "'3F', 63, 'Maximum positive (0x3F = 63).'", |
| 66 | + "'40', -64, '0x40 = 64 becomes 64–128 = –64.'", |
| 67 | + "'7F', -1, '0x7F = 127 becomes 127–128 = –1.'", |
| 68 | + "'8040', 64, 'Groups: 1 and 0 → (1<<7) + 0 = 128.'", |
| 69 | + "'8148', 200, 'Groups: 1 and 0x48 (72) → (1<<7) + 72 = 128 + 72 = 200.'", |
| 70 | + "'FF1C', -100, 'Groups: 0xFF (127) and 0x1C (28) → (127<<7) + 28 = 16284; since 16284 ≥ 8192, subtract 16384: 16284–16384 = –100.'", |
| 71 | + "'819C20', 20000, 'Groups: 1, 28, 32 → (1<<14) + (28<<7) + 32 = 16384 + 3584 + 32 = 20000.'", |
| 72 | + "'FEE360', -20000, 'Groups: 0xFE (126), 0xE3 (99), 0x60 (96) → (126<<14) + (99<<7) + 96 = 2077152; 2077152 – 2097152 = –20000.'", |
| 73 | + "'82B19640', 5000000, 'Groups: 2, 49, 22, 64 → (2<<21) + (49<<14) + (22<<7) + 64 = 5000000.'", |
| 74 | + "'FDCEE940', -5000000, 'Groups: 0xFD (125), 0xCF (79), 0xA8 (40), 0x00 (0) → (125<<21) + (79<<14) + (40<<7) + 0 = 263435456; 263435456 – 268435456 = –5000000.'", |
| 75 | + "'BFFFFF7F', 134217727, 'Maximum positive value in 28 bits. (Groups: 63, 127, 127, 127.)'", |
| 76 | + "'C0808000', -134217728, 'Minimum negative value in 28 bits. (Groups: 64, 0, 0, 0 → 64<<21 = 134217728; then 134217728 – 268435456 = –134217728.)'", |
| 77 | + }) |
| 78 | + void testVarIntRoundtrip(String hexString, long expectedValue, String description) throws Exception { |
| 79 | + byte[] serialized = Hex.decodeHex(hexString); |
| 80 | + |
| 81 | + // Parse the given array into a value |
| 82 | + ReadBufferByteBased readBuffer = new ReadBufferByteBased(serialized); |
| 83 | + long value = readBuffer.readLong("", 32, WithOption.WithEncoding("VARDINT")); |
| 84 | + assertEquals(expectedValue, value); |
| 85 | + |
| 86 | + // Serialize the given value into a byte array |
| 87 | + WriteBufferByteBased buffer = new WriteBufferByteBased(serialized.length); |
| 88 | + buffer.writeLong("", 32, expectedValue, WithOption.WithEncoding("VARDINT")); |
| 89 | + byte[] result = buffer.getBytes(); |
| 90 | + assertArrayEquals(serialized, result, description); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments