From 1df7caea696f90d570ebc7984570a57ba44ae5b6 Mon Sep 17 00:00:00 2001 From: Sean Copenhaver Date: Fri, 26 Jul 2013 16:24:00 -0400 Subject: [PATCH] adding a few tests for rebalancing --- lib/rope.ex | 2 +- test/rope_test.exs | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/rope.ex b/lib/rope.ex index dc59b78..7930bf1 100644 --- a/lib/rope.ex +++ b/lib/rope.ex @@ -155,7 +155,7 @@ defmodule Rope do defp flatten(leaves, rnode(right: right, left: left)) do leaves - |> flatten(right) + |> flatten(right) |> flatten(left) end diff --git a/test/rope_test.exs b/test/rope_test.exs index 32dc29e..438706f 100644 --- a/test/rope_test.exs +++ b/test/rope_test.exs @@ -99,6 +99,12 @@ defmodule RopeTest do is_equal Rope.slice(rope, 231, 15), String.slice(@longtext, 231, 15) end + test "rebalancing shouldn't effect a slice" do + rope = build_rope @longtext + rope = Rope.rebalance(rope) + is_equal Rope.slice(rope, 231, 15), String.slice(@longtext, 231, 15) + end + test "get the length of a rope" do rope = build_rope @longtext assert Rope.length(rope) == String.length(@longtext) @@ -127,6 +133,27 @@ defmodule RopeTest do assert Rope.depth(rope) == 8 end + test "rebalancing a balanced tree should return a rope of equal value" do + rope = build_rope @longtext + rope1= Rope.rebalance rope + rope2 = Rope.rebalance rope1 + + is_equal rope1, rope2 + assert Rope.depth(rope1) == Rope.depth(rope2) + end + + test "rebalancing can be called explicitly anytime" do + rope = @longtext + |> build_rope + |> Rope.rebalance + + rope = Rope.concat(rope, build_rope(@longtext)) + ropebalanced = Rope.rebalance(rope) + + is_equal rope, ropebalanced + end + + defp build_rope(text) do words = text |> String.split(" ") @@ -138,10 +165,18 @@ defmodule RopeTest do |> Enum.reduce(Rope.new(first), fn (word, rope) -> Rope.concat(rope, " " <> word) end) end - defp is_equal(rope, str) do + defp is_equal(rope, str) + when is_record(rope, Rope) and is_binary(str) do + assert Rope.length(rope) == String.length(str) assert rope_value(rope) == str end + defp is_equal(rope1, rope2) + when is_record(rope1, Rope) and is_record(rope2, Rope) do + assert Rope.length(rope1) == Rope.length(rope2) + assert rope_value(rope1) == rope_value(rope2) + end + defp rope_value(rope) do Kernel.inspect rope end