-module(binary_prop). -export([prop_hex_encode_2/0]). -compile([export_all, nowarn_export_all]). -include_lib("common_test/include/ct_property_test.hrl"). prop_hex_encode_2() -> ?FORALL(Data, data(), begin UpperHex = binary:encode_hex(Data, uppercase), LowerHex = binary:encode_hex(Data, lowercase), binary:decode_hex(LowerHex) =:= Data andalso binary:decode_hex(UpperHex) =:= Data andalso check_hex_encoded(Data, UpperHex, LowerHex) end). data() -> ?SIZED(Size, resize(Size * 2, binary())). %% @doc Credit to the comment of @Maria-12648430 -spec check_hex_encoded(Data :: binary(), UpperHex :: binary(), LoweHex :: binary()) -> boolean(). check_hex_encoded(<>, <>, <>) -> check_hex_chars_match(I1, U1, L1) andalso check_hex_chars_match(I2, U2, L2) andalso check_hex_encoded(Ins, UCs, LCs); check_hex_encoded(<<>>, <<>>, <<>>) -> true; check_hex_encoded(_, _, _) -> false. check_hex_chars_match(X, U, L) when X < 10 -> (U =:= $0 + X) andalso (L =:= $0 + X); check_hex_chars_match(X, U, L) -> (U =:= $A + X -10) andalso (L =:= $a + X -10).