git.mcksp
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
require "./const"
require "colorize"

module Fork
  module Benchmark
    extend self

    def run
      board = Board.new("k7/7P/5K1/7q/8/8/8/8 b - - 0 1")
      run_benchmark("queen not attacking", board, [{H5, H7}])

      board = Board.new("r5rk/5p1p/5R2/4B3/8/8/7P/7K w - - 0 1")
      run_benchmark("discovered checkmate", board, [{F6, A6}, {F7, F6}, {E5, F6}, {G8, G7}, {A6, A8}])

      board = Board.new("R2qkbnr/3n1ppp/3P4/8/4P3/1p3N1P/2P2PP1/1NBQ1RK1 b - - 0 1")
      run_benchmark("take that rook fast bro", board, [{D8, A8}])

      board = Board.new("kb1r4/b2p4/8/2P5/8/5Q2/8/2K5 b - - 0 1")
      run_benchmark("double push & en passant", board, [{D7, D5}, {C5, D6}])
    end

    private def run_benchmark(name, board, moves)
      engine_moves = [] of Move
      elapsed_time = Time.measure do
        moves.size.times do
          res = Search.new(board, 6).run
          if m = res.first
            board.make_move(m)
            engine_moves << m
          end
        end
      end

      puts "#{name}: time: #{elapsed_time}"
      moves.each_with_index do |m, i|
        from, to = m
        em = engine_moves[i]
        ok = from == em.from && to == em.to
        status_test = ok ? "OK".colorize(:green) : "WRONG".colorize(:red)
        puts "#{status_test}: wanted: #{Fork.square_to_text(from)}-#{Fork.square_to_text(to)} was: #{Fork.square_to_text(em.from)}-#{Fork.square_to_text(em.to)}"
      end
    end
  end
end