-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
defmodule RedBlackTreePropertyBasedTest do | ||
use ExUnit.Case | ||
use ExUnitProperties | ||
|
||
alias RedBlackTree | ||
|
||
property "insertion maintains red-black properties for any sequence of integers" do | ||
check all( | ||
keys <- uniq_list_of(integer()), | ||
values <- list_of(term()), | ||
max_runs: 100 | ||
) do | ||
# Обрезаем values до длины keys | ||
values = Enum.take(values, length(keys)) | ||
|
||
# Создаём список пар {key, value} | ||
kv_pairs = Enum.zip(keys, values) | ||
|
||
# Вставляем пары в дерево | ||
tree = | ||
Enum.reduce(kv_pairs, nil, fn {key, value}, acc -> | ||
RedBlackTree.insert(acc, key, value) | ||
end) | ||
|
||
# Проверяем корректность красно-чёрного дерева | ||
assert tree == nil or RedBlackTree.valid_red_black_tree?(tree), | ||
"Дерево должно удовлетворять свойствам красно-чёрного дерева" | ||
end | ||
end | ||
|
||
property "inserting and retrieving values works correctly" do | ||
check all( | ||
keys <- uniq_list_of(integer()), | ||
values <- list_of(term()), | ||
max_runs: 100 | ||
) do | ||
values = Enum.take(values, length(keys)) | ||
kv_pairs = Enum.zip(keys, values) | ||
|
||
tree = | ||
Enum.reduce(kv_pairs, nil, fn {key, value}, acc -> | ||
RedBlackTree.insert(acc, key, value) | ||
end) | ||
|
||
Enum.each(kv_pairs, fn {key, value} -> | ||
assert RedBlackTree.get(tree, key) == value | ||
end) | ||
end | ||
end | ||
end |
2 changes: 1 addition & 1 deletion
2
test/red_black_tree_test.exs → test/red_black_tree_unit_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
defmodule RedBlackTreeTest do | ||
defmodule RedBlackTreeUnitTest do | ||
use ExUnit.Case | ||
alias RedBlackTree | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule TreeDictPropertyBasedTest do | ||
use ExUnit.Case | ||
use ExUnitProperties | ||
|
||
alias TreeDict | ||
|
||
property "TreeDict insert and get work correctly" do | ||
check all( | ||
keys <- uniq_list_of(term()), | ||
values <- list_of(term()), | ||
max_runs: 100 | ||
) do | ||
values = Enum.take(values, length(keys)) | ||
kv_pairs = Enum.zip(keys, values) | ||
|
||
dict = | ||
Enum.reduce(kv_pairs, TreeDict.new(), fn {key, value}, acc -> | ||
TreeDict.insert(acc, key, value) | ||
end) | ||
|
||
Enum.each(kv_pairs, fn {key, value} -> | ||
assert TreeDict.get(dict, key) == value | ||
end) | ||
end | ||
end | ||
end |