/var/log/messages

Jul 23, 2019 - 2 minute read - Comments - programming

Game of Life の実装 (4)

テスツ、良い情報を見つけられていなかった (ググリ力がアレ) のですが以下を発見。

割と簡単そう、ということで例の Game of Life のテスツを書いてみることに。

ひとまず

Cell を読みつつ控えを取りつつ試験を書いていく方向にて。以下なカンジのナニがでっち上がっています。

defmodule CellTest do
  use ExUnit.Case
  doctest Cell
  setup do
    {:ok,server_pid} = Cell.start_link({0, 0})
    {:ok,server: server_pid}
  end

  test "info test", %{server: pid} do
    assert {0, 0, 0} == Cell.info(pid)
  end

  test "info test (one neighbour)", %{server: pid} do
    {:ok, _another_server_pid} = Cell.start_link({-1, 0})
    assert {0, 0, 1} == Cell.info(pid)
  end

  test "lookup test (returns nil)" do
    assert nil == Cell.lookup({1, 1})
  end

  test "lookup test (returns {0, 0})", %{server: pid} do
    assert pid == Cell.lookup({0, 0})
  end

  test "count_neighbours test (returns 0)", %{server: pid} do
    assert 0 == Cell.count_neighbours(pid)
  end

  test "count_neighbours test (returns 1)", %{server: pid} do
    {:ok, _another_server_pid} = Cell.start_link({-1, 0})
    assert 1 == Cell.count_neighbours(pid)
  end

  test "position test", %{server: pid} do
    assert {0, 0} == Cell.position(pid)
  end

  test "tick test (no neighbour)", %{server: pid} do
    assert {[pid], []} == Cell.tick(pid)
  end

  test "tick test (1 neighbour)", %{server: pid} do
    {:ok, _another_server_pid1} = Cell.start_link({-1, 0})
    assert {[pid], []} == Cell.tick(pid)
  end

  test "tick test (2 neighbour)", %{server: pid} do
    {:ok, _another_server_pid1} = Cell.start_link({-1, 0})
    {:ok, _another_server_pid2} = Cell.start_link({-1, -1})
    assert {[], [{0, -1}]} == Cell.tick(pid)
  end

  test "tick test (3 neighbour)", %{server: pid} do
    {:ok, _another_server_pid1} = Cell.start_link({-1, 0})
    {:ok, _another_server_pid2} = Cell.start_link({-1, -1})
    {:ok, _another_server_pid3} = Cell.start_link({1, 1})
    assert {[], [{0, -1}, {0, 1}]} == Cell.tick(pid)
  end

  test "tick test (4 neighbour)", %{server: pid} do
    {:ok, _another_server_pid1} = Cell.start_link({-1, 0})
    {:ok, _another_server_pid2} = Cell.start_link({-1, -1})
    {:ok, _another_server_pid3} = Cell.start_link({1, 1})
    {:ok, _another_server_pid4} = Cell.start_link({1, -1})
    assert {[pid], [{1, 0}, {0, 1}]} == Cell.tick(pid)
  end

  test "tick test (5 neighbour)", %{server: pid} do
    {:ok, _another_server_pid1} = Cell.start_link({-1, 0})
    {:ok, _another_server_pid2} = Cell.start_link({-1, -1})
    {:ok, _another_server_pid3} = Cell.start_link({1, 1})
    {:ok, _another_server_pid4} = Cell.start_link({1, -1})
    {:ok, _another_server_pid5} = Cell.start_link({0, -1})
    assert {[pid], [{0, 1}]} == Cell.tick(pid)
  end

  test "tick test (6 neighbour)", %{server: pid} do
    {:ok, _another_server_pid1} = Cell.start_link({-1, 0})
    {:ok, _another_server_pid2} = Cell.start_link({-1, -1})
    {:ok, _another_server_pid3} = Cell.start_link({1, 1})
    {:ok, _another_server_pid4} = Cell.start_link({1, -1})
    {:ok, _another_server_pid5} = Cell.start_link({0, -1})
    {:ok, _another_server_pid6} = Cell.start_link({0, 1})
    assert {[pid], [{-1, 1}]} == Cell.tick(pid)
  end

  test "tick test (7 neighbour)", %{server: pid} do
    {:ok, _another_server_pid1} = Cell.start_link({-1, 0})
    {:ok, _another_server_pid2} = Cell.start_link({-1, -1})
    {:ok, _another_server_pid3} = Cell.start_link({1, 1})
    {:ok, _another_server_pid4} = Cell.start_link({1, -1})
    {:ok, _another_server_pid5} = Cell.start_link({0, -1})
    {:ok, _another_server_pid6} = Cell.start_link({0, 1})
    {:ok, _another_server_pid7} = Cell.start_link({1, 0})
    assert {[pid], [{-1, 1}]} == Cell.tick(pid)
  end

  test "tick test (8 neighbour)", %{server: pid} do
    {:ok, _another_server_pid1} = Cell.start_link({-1, 0})
    {:ok, _another_server_pid2} = Cell.start_link({-1, -1})
    {:ok, _another_server_pid3} = Cell.start_link({1, 1})
    {:ok, _another_server_pid4} = Cell.start_link({1, -1})
    {:ok, _another_server_pid5} = Cell.start_link({0, -1})
    {:ok, _another_server_pid6} = Cell.start_link({0, 1})
    {:ok, _another_server_pid7} = Cell.start_link({1, 0})
    {:ok, _another_server_pid8} = Cell.start_link({-1, 1})
    assert {[pid], []} == Cell.tick(pid)
  end
end

reap とか sow については Supervisor との連携が出てくるのでこのあたりが限界なのかどうか。

Game of Life の実装 (3) IEx.pry 試してみた

comments powered by Disqus