Ignore:
Timestamp:
2023-09-12T18:04:57+02:00 (19 months ago)
Author:
taylor.smock
Message:

Fix #23165: Cannot assign a changesetId > 0 to a new primitive

There were two problems:

  1. Using Number.intValue instead of Number.longValue
  2. Using >> instead of >>> (bit shift)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/data/protobuf/ProtobufParserTest.java

    r17867 r18826  
    44import static org.junit.jupiter.api.Assertions.assertEquals;
    55
     6import java.util.stream.Stream;
     7
    68import org.junit.jupiter.api.Test;
     9import org.junit.jupiter.params.ParameterizedTest;
     10import org.junit.jupiter.params.provider.Arguments;
     11import org.junit.jupiter.params.provider.MethodSource;
    712
    813/**
     
    4954        assertEquals(4_294_967_297L, ProtobufParser.encodeZigZag(Integer.MIN_VALUE - 1L).longValue());
    5055    }
     56
     57    static Stream<Arguments> testDecode() {
     58        return Stream.of(
     59                Arguments.of(21L, 42L),
     60                Arguments.of(9223372036854775785L, -46L)
     61        );
     62    }
     63
     64    @ParameterizedTest
     65    @MethodSource
     66    void testDecode(long expected, long toDecode) {
     67        assertEquals(expected, ProtobufParser.decodeZigZag(toDecode));
     68    }
     69
     70    static Stream<Arguments> testDecodeVarInt() {
     71        return Stream.of(
     72                Arguments.of(1, new int[] {0x01}),
     73                Arguments.of(150, new int[] {0x96, 0x01}),
     74                Arguments.of(9223372036854775806L, new int[] {0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F}),
     75                Arguments.of(Long.MAX_VALUE, new int[] {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F})
     76        );
     77    }
     78
     79    @ParameterizedTest
     80    @MethodSource
     81    void testDecodeVarInt(long expected, int[] bytes) {
     82        // Drop most significant bit
     83        for (int i = 0; i < bytes.length; i++) {
     84            bytes[i] = bytes[i] & 0x7F;
     85        }
     86        assertEquals(expected, ProtobufParser.convertByteArray(ProtobufTest.toByteArray(bytes), (byte) 7, 0, bytes.length));
     87    }
    5188}
Note: See TracChangeset for help on using the changeset viewer.