Skip to content

Commit 27f9e39

Browse files
committed
Merge remote-tracking branch 'origin/master' into release
2 parents c2cc95b + 3a091b8 commit 27f9e39

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

kore/src/Kore/Builtin/InternalBytes.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ bytes2int bytes end sign =
529529
case sign of
530530
Unsigned _ -> unsigned
531531
Signed _
532-
| 2 * unsigned > modulus -> unsigned - modulus
532+
| 2 * unsigned >= modulus -> unsigned - modulus
533533
| otherwise -> unsigned
534534
where
535535
(modulus, unsigned) = ByteString.foldl' go (1, 0) littleEndian

kore/test/Test/Kore/Builtin/InternalBytes.hs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -599,21 +599,23 @@ testBadEvaluation testName hook term =
599599
assertEqual "" expectedErrMsg errMsg
600600

601601
int2bytesData ::
602-
-- | (integer, big endian?, bytes)
603-
[(Integer, Bool, ByteString)]
602+
-- | (unsigned integer, signed integer, big endian?, bytes)
603+
[(Integer, Integer, Bool, ByteString)]
604604
int2bytesData =
605-
[ (0, True, "\x00\x00\x00\x00")
606-
, (128, True, "\x80")
607-
, (-128, True, "\x80")
608-
, (2, True, "\x02")
609-
, (-2, True, "\xfe")
610-
, (16, True, "\x10")
611-
, (-16, True, "\xf0")
612-
, (128, True, "\x00\x80")
613-
, (-128, True, "\xff\x80")
614-
, (128, False, "\x80\x00")
615-
, (-128, False, "\x80\xff")
616-
, (0, True, "")
605+
[ (0, 0, True, "\x00\x00\x00\x00")
606+
, (128, -128, True, "\x80")
607+
, (2, 2, True, "\x02")
608+
, (254, -2, True, "\xfe")
609+
, (255, -1, True, "\xff")
610+
, (16, 16, True, "\x10")
611+
, (240, -16, True, "\xf0")
612+
, (128, 128, True, "\x00\x80")
613+
, (65408, -128, True, "\xff\x80")
614+
, (32768, -32768, True, "\x80\x00")
615+
, (128, 128, False, "\x80\x00")
616+
, (65408, -128, False, "\x80\xff")
617+
, (32768, -32768, False, "\x00\x80")
618+
, (0, 0, True, "")
617619
]
618620

619621
test_int2bytes :: [TestTree]
@@ -622,9 +624,9 @@ test_int2bytes =
622624
where
623625
test ::
624626
HasCallStack =>
625-
(Integer, Bool, ByteString) ->
627+
(Integer, Integer, Bool, ByteString) ->
626628
TestTree
627-
test (integer, bigEndian, bytes) =
629+
test (integer, signed, bigEndian, bytes) =
628630
testCase name $ do
629631
let input =
630632
int2bytes
@@ -634,6 +636,14 @@ test_int2bytes =
634636
expect = [asPattern bytes]
635637
actual <- simplify input
636638
assertEqual "" expect actual
639+
-- converting a signed integer should have the same result
640+
let signedInput =
641+
int2bytes
642+
(Test.Int.asInternal len)
643+
(Test.Int.asInternal signed)
644+
end
645+
signedActual <- simplify signedInput
646+
assertEqual "" expect signedActual
637647
where
638648
name =
639649
let args =
@@ -653,23 +663,20 @@ test_bytes2int =
653663
where
654664
test ::
655665
HasCallStack =>
656-
(Integer, Bool, ByteString) ->
666+
(Integer, Integer, Bool, ByteString) ->
657667
TestTree
658-
test (integer, bigEndian, bytes) =
668+
test (unsigned, signed, bigEndian, bytes) =
659669
testGroup name (mkCase <$> [True, False])
660670
where
661-
mkCase signed =
662-
testCase (if signed then "signed" else "unsigned") $ do
671+
mkCase testSigned =
672+
testCase (if testSigned then "signed" else "unsigned") $ do
663673
let sign
664-
| signed = signedBytes
674+
| testSigned = signedBytes
665675
| otherwise = unsignedBytes
666-
underflow = (-2) * integer >= modulus
667-
int
668-
| not signed, integer < 0 = integer + modulus
669-
| signed, underflow = integer + modulus
670-
| otherwise = integer
671-
modulus = 0x100 ^ ByteString.length bytes
672676
input = bytes2int (asInternal bytes) end sign
677+
int
678+
| testSigned = signed
679+
| otherwise = unsigned
673680
expect =
674681
[ Test.Int.asPattern int
675682
& Pattern.mapVariables (pure mkConfigVariable)

0 commit comments

Comments
 (0)