diff --git a/mix.exs b/mix.exs index 573747a..0689ce0 100644 --- a/mix.exs +++ b/mix.exs @@ -37,7 +37,10 @@ defmodule Thrash.Mixfile do only: :bench, tag: "f6394871e5685aa1c7e125f198dead0c8a15e992"}, {:exprof, "~>0.2", only: :bench}, - {:benchwarmer, "~>0.0.2", only: :bench}] + {:benchwarmer, "~>0.0.2", only: :bench}, + {:excheck, "~>0.3.0", only: :test}, + {:triq, github: "krestenkrab/triq", only: :test} + ] end defp description do diff --git a/mix.lock b/mix.lock index 01a4251..317de24 100644 --- a/mix.lock +++ b/mix.lock @@ -1,12 +1,14 @@ %{"aleppo": {:git, "https://github.com/ChicagoBoss/aleppo.git", "e5af421b8c75d86dd88aefae91402478250bb82c", [tag: "v0.9"]}, - "benchwarmer": {:hex, :benchwarmer, "0.0.2"}, - "bunt": {:hex, :bunt, "0.1.5"}, - "credo": {:hex, :credo, "0.3.12"}, - "dialyze": {:hex, :dialyze, "0.2.1"}, - "earmark": {:hex, :earmark, "0.2.1"}, - "ex_doc": {:hex, :ex_doc, "0.11.4"}, - "exprintf": {:hex, :exprintf, "0.1.6"}, - "exprof": {:hex, :exprof, "0.2.0"}, + "benchwarmer": {:hex, :benchwarmer, "0.0.2", "902e5c020608647b07c38b82103e4af6d2667dfd5d5d13c67382238de6943136", [:mix], []}, + "bunt": {:hex, :bunt, "0.1.5", "c378ea1698232597d3778e4b83234dcea4a60e7c28114b0fe53657a2c0d8885e", [:mix], []}, + "credo": {:hex, :credo, "0.3.12", "b1e85ed83f8c2daa6858335dc0bfac25bea5fdc69edf84fe3a4cc846f5ac7d8f", [:mix], [{:bunt, "~> 0.1.4", [hex: :bunt, optional: false]}]}, + "dialyze": {:hex, :dialyze, "0.2.1", "9fb71767f96649020d769db7cbd7290059daff23707d6e851e206b1fdfa92f9d", [:mix], []}, + "earmark": {:hex, :earmark, "0.2.1", "ba6d26ceb16106d069b289df66751734802777a3cbb6787026dd800ffeb850f3", [:mix], []}, + "ex_doc": {:hex, :ex_doc, "0.11.4", "a064bdb720594c3745b94709b17ffb834fd858b4e0c1f48f37c0d92700759e02", [:mix], [{:earmark, "~> 0.1.17 or ~> 0.2", [hex: :earmark, optional: true]}]}, + "excheck": {:hex, :excheck, "0.3.3", "ce2353afc7616cb8e41e03246448a2ca3d31c1492366687927f4dfaba21c155b", [:mix], []}, + "exprintf": {:hex, :exprintf, "0.1.6", "b5b0a38bf78a357dbc61cdf7364fea004af6fdf5214be44021eb2ea7edf61f6e", [:mix], []}, + "exprof": {:hex, :exprof, "0.2.0", "b03f50d0d33e2f18c8e047d9188ba765dc32daba0b553ed717a98a78561d5eaf", [:mix], [{:exprintf, "~> 0.1", [hex: :exprintf, optional: false]}]}, "quaff": {:git, "https://github.com/qhool/quaff.git", "9a4ba378d470beac708e366dc9bacd5a9ef6f016", [tag: "9a4ba378d470beac708e366dc9bacd5a9ef6f016"]}, "thrift": {:git, "https://github.com/apache/thrift", "591e20f9636c37527a70dc03598218c3468a0eff", [tag: "0.9.2"]}, - "thrift_ex": {:git, "https://github.com/dantswain/thrift_ex.git", "3fe3341f9dc34219ceb04ae2d3751b9fb5eac37a", [tag: "f6394871e5685aa1c7e125f198dead0c8a15e992"]}} + "thrift_ex": {:git, "https://github.com/dantswain/thrift_ex.git", "3fe3341f9dc34219ceb04ae2d3751b9fb5eac37a", [tag: "f6394871e5685aa1c7e125f198dead0c8a15e992"]}, + "triq": {:git, "https://github.com/krestenkrab/triq.git", "c7306b8eaea133d52140cb828817efb5e50a3d52", []}} diff --git a/test/test_helper.exs b/test/test_helper.exs index 869559e..13af10d 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1 +1,2 @@ +ExCheck.start() ExUnit.start() diff --git a/test/thrash/binary_properties_test.exs b/test/thrash/binary_properties_test.exs new file mode 100644 index 0000000..e4a1ef2 --- /dev/null +++ b/test/thrash/binary_properties_test.exs @@ -0,0 +1,98 @@ +defmodule Thrash.BinaryPropertiesTest do + use ExUnit.Case, async: false + use ExCheck + + @max_signed_byte trunc(:math.pow(2, 4) - 1) + @max_signed_i16 trunc(:math.pow(2, 8) - 1) + @max_signed_i32 trunc(:math.pow(2, 16) - 1) + @max_signed_i64 trunc(:math.pow(2, 32) - 1) + + def cycle_serialization(module, value) do + value + |> module.serialize + |> module.deserialize + end + + property "SubStruct" do + for_all { + sub_id, + sub_name + } in { + oneof([choose(-@max_signed_i32, @max_signed_i32), nil]), + oneof([unicode_binary, nil]) + } do + sub_struct = %SubStruct{sub_id: sub_id, sub_name: sub_name} + {got_sub_struct, ""} = SubStruct.deserialize(SubStruct.serialize(sub_struct)) + sub_struct == got_sub_struct + end + end + + @tag iterations: 200 + property "SimpleStruct" do + for_all { + id, + name, + list_of_ints, + bigint, + sub_id, + sub_name, + flag, + floatval, + taco_pref, + list_of_structs, + chew, + mediumint, + map_int_to_string, + map_string_to_struct, + set_of_strings, + } in { + oneof([choose(-@max_signed_i32, @max_signed_i32), nil]), + oneof([unicode_binary, nil]), + list(choose(-@max_signed_i32, @max_signed_i32)), + oneof([choose(-@max_signed_i64, @max_signed_i64), nil]), + oneof([choose(-@max_signed_i32, @max_signed_i32), nil]), + oneof([unicode_binary, nil]), + bool, + oneof([real, nil]), + elements(TacoType.atoms), + list({int, unicode_binary}), + oneof([choose(-@max_signed_byte, @max_signed_byte), nil]), + oneof([choose(-@max_signed_i16, @max_signed_i16), nil]), + list({choose(-@max_signed_i32, @max_signed_i32), unicode_binary}), + list({unicode_binary, int, unicode_binary}), + list(unicode_binary), + } do + sub_struct = %SubStruct{sub_id: sub_id, sub_name: sub_name} + list_of_structs = list_of_structs + |> Enum.map(fn({sid, sname}) -> + %SubStruct{sub_id: sid, sub_name: sname} + end) + map_int_to_string = Enum.into(map_int_to_string, %{}) + map_string_to_struct = map_string_to_struct + |> Enum.reduce(%{}, fn({key, sid, sname}, acc) -> + Map.put(acc, key, %SubStruct{sub_id: sid, sub_name: sname}) + end) + set_of_strings = set_of_strings + |> Enum.reduce(MapSet.new, fn(el, acc) -> + MapSet.put(acc, el) + end) + struct = %SimpleStruct{ + id: id, + name: name, + list_of_ints: list_of_ints, + sub_struct: sub_struct, + flag: flag, + floatval: floatval, + taco_pref: taco_pref, + list_of_structs: list_of_structs, + chew: chew, + mediumint: mediumint, + map_int_to_string: map_int_to_string, + map_string_to_struct: map_string_to_struct, + set_of_strings: set_of_strings + } + {got_struct, ""} = cycle_serialization(SimpleStruct, struct) + struct == got_struct + end + end +end