Add binary search to Util
For some reason there doesn't appear to be a binary search function in Elixir's standard library, so this implements that. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
12
test/contrib/markov/chain_test.exs
Normal file
12
test/contrib/markov/chain_test.exs
Normal file
@@ -0,0 +1,12 @@
|
||||
defmodule MarkovChainTest do
|
||||
use ExUnit.Case
|
||||
alias Omnibot.Contrib.Markov.Chain
|
||||
|
||||
test "chain train_one works correctly" do
|
||||
chain = %Chain {order: 2}
|
||||
|> Chain.train_one(["foo", "bar"], "baz")
|
||||
#assert chain.chain == [
|
||||
#{["foo", "bar"], {"baz", 1}}
|
||||
#]
|
||||
end
|
||||
end
|
||||
@@ -12,4 +12,21 @@ defmodule Omnibot.UtilTest do
|
||||
assert Util.string_or_nil("") == nil
|
||||
assert Util.string_or_nil("asdf") == "asdf"
|
||||
end
|
||||
|
||||
test "binary_search" do
|
||||
indexes = 0..10 |> Enum.to_list()
|
||||
values = indexes |> Enum.map(&({&1, &1 * 2}))
|
||||
assert Enum.map(indexes, &(Util.binary_search(values, &1))) == values
|
||||
|
||||
indexes = 0..101 |> Enum.to_list()
|
||||
values = indexes |> Enum.map(&({&1, &1 * 2}))
|
||||
assert Enum.map(indexes, &(Util.binary_search(values, &1))) == values
|
||||
|
||||
values = [a: 15, b: 22, c: -1, d: 0]
|
||||
|
||||
assert Util.binary_search(values, :a) == {0, 15}
|
||||
assert Util.binary_search(values, :b) == {1, 22}
|
||||
assert Util.binary_search(values, :c) == {2, -1}
|
||||
assert Util.binary_search(values, :d) == {3, 0}
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user