From 90737a5602f594e0252656994bb5ee6ec77b94e2 Mon Sep 17 00:00:00 2001 From: Francisco Rojas Date: Thu, 24 Dec 2015 13:38:43 -0800 Subject: [PATCH 1/2] Add excheck as dependency * Also, commit mix.lock file --- config/config.exs | 2 ++ mix.exs | 5 ++++- mix.lock | 2 ++ test/test_helper.exs | 1 + 4 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 mix.lock diff --git a/config/config.exs b/config/config.exs index 6dfa82f..dff0e79 100644 --- a/config/config.exs +++ b/config/config.exs @@ -2,6 +2,8 @@ # and its dependencies with the aid of the Mix.Config module. use Mix.Config +config :excheck, :number_iterations, 200 + # This configuration is loaded before any dependency and is restricted # to this project. If another project depends on this project, this # file won't be loaded nor affect the parent project. For this reason, diff --git a/mix.exs b/mix.exs index f960a17..44657d1 100644 --- a/mix.exs +++ b/mix.exs @@ -27,7 +27,10 @@ defmodule Heapq.Mixfile do # # Type `mix help deps` for more examples and options defp deps do - [] + [ + {:excheck, "~> 0.3", only: :test}, + {:triq, github: "krestenkrab/triq", only: :test} + ] end defp package do diff --git a/mix.lock b/mix.lock new file mode 100644 index 0000000..b1e612c --- /dev/null +++ b/mix.lock @@ -0,0 +1,2 @@ +%{"excheck": {:hex, :excheck, "0.3.2"}, + "triq": {:git, "https://github.com/krestenkrab/triq.git", "c7306b8eaea133d52140cb828817efb5e50a3d52", []}} diff --git a/test/test_helper.exs b/test/test_helper.exs index 869559e..b1a1d61 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1 +1,2 @@ +ExCheck.start ExUnit.start() From f53a9fecd6e54951dec7b141b353dceff7d8f705 Mon Sep 17 00:00:00 2001 From: Francisco Rojas Date: Thu, 24 Dec 2015 13:39:51 -0800 Subject: [PATCH 2/2] Add propery based test properties: * empty? * find min element * elements are pop in a sorted fashion * to_list generate a sorted list * size --- test/heapq_prop_test.exs | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 test/heapq_prop_test.exs diff --git a/test/heapq_prop_test.exs b/test/heapq_prop_test.exs new file mode 100644 index 0000000..1287cff --- /dev/null +++ b/test/heapq_prop_test.exs @@ -0,0 +1,58 @@ +defmodule HeapQueue.PropTest do + use ExUnit.Case, async: false + use ExCheck + + property :empty? do + for_all {x, y} in {int, int} do + q = HeapQueue.push(HeapQueue.new(), x, y) + {{:value, ^x, value}, q} = HeapQueue.pop(q) + value == y && HeapQueue.empty?(q) + end + end + + property :find_min do + for_all {x, y} in such_that({xx, yy} in {int, int} when xx < yy) do + q = HeapQueue.new() + |> HeapQueue.push_value(x) + |> HeapQueue.push_value(y) + + {{:value, value}, _q} = HeapQueue.pop_value(q) + value == x + end + end + + def sorted?(q) do + if(HeapQueue.empty?(q)) do + true + else + {{:value, m}, q} = HeapQueue.pop_value(q) + if(HeapQueue.empty?(q)) do + true + else + {{:value, value}, _q} = HeapQueue.pop_value(q) + (m <= value && sorted?(q)) + end + end + end + + property :sorted do + for_all xs in list(int) do + q = Enum.reduce(xs, HeapQueue.new(), &(HeapQueue.push_value(&2, &1))) + sorted?(q) + end + end + + property :to_list_sorted do + for_all xs in list(int) do + q = Enum.reduce(xs, HeapQueue.new(), &(HeapQueue.push_value(&2, &1))) + HeapQueue.to_list_of_values(q) == Enum.sort(xs) + end + end + + property :size do + for_all xs in list(int) do + q = Enum.reduce(xs, HeapQueue.new(), &(HeapQueue.push_value(&2, &1))) + HeapQueue.size(q) == Enum.count(xs) + end + end +end