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
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
//! Implements `StdSearchNode`.

use std::cmp::min;
use std::cell::UnsafeCell;
use std::hash::Hasher;
use std::collections::hash_map::DefaultHasher;
use uci::{SetOption, OptionDescription};
use board::{Board, IllegalBoard};
use value::*;
use depth::*;
use qsearch::{Qsearch, QsearchParams, QsearchResult};
use moves::{Move, MoveDigest, AddMove};
use move_generator::MoveGenerator;
use search_node::SearchNode;
use utils::{ZobristArrays, parse_fen};


/// Contains information about a position.
#[derive(Clone, Copy)]
struct PositionInfo {
    /// The number of half-moves since the last piece capture or pawn
    /// advance. (We do not allow `halfmove_clock` to become greater
    /// than 99.)
    halfmove_clock: u8,

    /// The last played move.
    last_move: Move,
}


/// Implements the `SearchNode` trait.
pub struct StdSearchNode<T: Qsearch> {
    zobrist: &'static ZobristArrays,
    position: UnsafeCell<T::MoveGenerator>,

    /// Information needed so as to be able to undo the played moves.
    state_stack: Vec<PositionInfo>,

    /// The count of half-moves since the beginning of the game.
    halfmove_count: u16,

    /// `true` if the position is deemed as a draw by repetition or
    /// because 50 moves have been played without capturing a piece or
    /// advancing a pawn.
    repeated_or_rule50: bool,

    /// The hash value for the underlying `Board` instance.
    board_hash: u64,

    /// A list of hash values for the `Board` instances that had
    /// occurred during the game. This is needed so as to be able to
    /// detect repeated positions.
    encountered_boards: Vec<u64>,

    /// A collective hash value for the set of boards that had
    /// occurred at least twice before the root position (the earliest
    /// position in `state_stack`), and can still be reached by
    /// playing moves from the root position. An empty set has a hash
    /// of `0`. We use this value when we generate position's hash.
    repeated_boards_hash: u64,
}


impl<T: Qsearch> SearchNode for StdSearchNode<T> {
    type Evaluator = <<T as Qsearch>::MoveGenerator as MoveGenerator>::Evaluator;

    type QsearchResult = T::QsearchResult;

    fn from_history(fen: &str, moves: &mut Iterator<Item = &str>) -> Result<Self, IllegalBoard> {
        let mut p: StdSearchNode<T> = try!(StdSearchNode::from_fen(fen));
        let mut move_list = Vec::new();
        'played_moves: for played_move in moves {
            move_list.clear();
            p.position().generate_all(&mut move_list);
            for m in move_list.iter() {
                if played_move == m.notation() {
                    if p.do_move(*m) {
                        continue 'played_moves;
                    }
                    break;
                }
            }
            return Err(IllegalBoard);
        }
        p.declare_as_root();
        Ok(p)
    }

    #[inline]
    fn hash(&self) -> u64 {
        // Notes:
        //
        // 1. Two positions that differ in their sets of previously
        //    repeated, still reachable boards will have different
        //    hashes.
        //
        // 2. Two positions that differ only in their number of played
        //    moves without capturing piece or advancing a pawn will
        //    have equal hashes, as long as they both are far from the
        //    rule-50 limit.

        if self.repeated_or_rule50 {
            // All repeated and rule-50 positions are a draw, so for
            // practical purposes they can be considered to be the
            // exact same position, and therefore we can generate the
            // same hash value for all of them. This has the important
            // practical advantage that we get two separate records in
            // the transposition table for the first and the second
            // occurrence of the same position. (The second occurrence
            // being deemed as a draw.)
            1
        } else {
            let hash = if self.root_is_reachable() {
                // If the repeated positions that occured before the
                // root postition are still reachable, we blend their
                // collective hash into current position's hash.
                self.board_hash ^ self.repeated_boards_hash
            } else {
                self.board_hash
            };
            let halfmove_clock = self.state().halfmove_clock;
            if halfmove_clock >= 70 {
                // If `halfmove_clock` is close to rule-50, we blend
                // it into the returned hash.
                hash ^ self.zobrist.halfmove_clock[halfmove_clock as usize]
            } else {
                hash
            }
        }
    }

    #[inline]
    fn board(&self) -> &Board {
        self.position().board()
    }

    #[inline]
    fn halfmove_clock(&self) -> u8 {
        self.state().halfmove_clock
    }

    #[inline]
    fn fullmove_number(&self) -> u16 {
        1 + (self.halfmove_count >> 1)
    }

    #[inline]
    fn is_check(&self) -> bool {
        self.position().is_check()
    }

    #[inline]
    fn evaluator(&self) -> &Self::Evaluator {
        self.position().evaluator()
    }

    #[inline]
    fn evaluate_final(&self) -> Value {
        if self.repeated_or_rule50 || !self.is_check() {
            0
        } else {
            VALUE_MIN
        }
    }

    #[inline]
    fn evaluate_move(&self, m: Move) -> Value {
        self.position().evaluate_move(m)
    }

    #[inline]
    fn qsearch(&self,
               depth: Depth,
               lower_bound: Value,
               upper_bound: Value,
               static_eval: Value)
               -> Self::QsearchResult {
        debug_assert!(DEPTH_MIN <= depth && depth <= 0);
        debug_assert!(lower_bound >= VALUE_MIN);
        debug_assert!(upper_bound <= VALUE_MAX);
        debug_assert!(lower_bound < upper_bound);
        if self.repeated_or_rule50 {
            Self::QsearchResult::new(0, 0)
        } else {
            T::qsearch(QsearchParams {
                           position: unsafe { self.position_mut() },
                           depth: depth,
                           lower_bound: lower_bound,
                           upper_bound: upper_bound,
                           static_eval: static_eval,
                       })
        }
    }

    #[inline]
    fn generate_moves<U: AddMove>(&self, moves: &mut U) {
        if !self.repeated_or_rule50 {
            self.position().generate_all(moves);
        }
    }

    #[inline]
    fn try_move_digest(&self, move_digest: MoveDigest) -> Option<Move> {
        if self.repeated_or_rule50 {
            None
        } else {
            self.position().try_move_digest(move_digest)
        }
    }

    #[inline]
    fn null_move(&self) -> Move {
        self.position().null_move()
    }

    #[inline]
    fn last_move(&self) -> Move {
        self.state().last_move
    }

    fn do_move(&mut self, m: Move) -> bool {
        if self.repeated_or_rule50 && m.is_null() {
            // This is a final position -- null moves are not
            // allowed. We must still allow other moves though,
            // because `from_history` should be able to call `do_move`
            // even in final positions.
            return false;
        }

        if let Some(h) = unsafe { self.position_mut().do_move(m) } {
            let halfmove_clock = if m.is_pawn_advance_or_capure() {
                0
            } else {
                match self.state().halfmove_clock {
                    x if x < 99 => x + 1,
                    _ => {
                        if !self.is_checkmate() {
                            self.repeated_or_rule50 = true;
                        }
                        99
                    }
                }
            };
            self.halfmove_count += 1;
            self.encountered_boards.push(self.board_hash);
            self.board_hash ^= h;
            debug_assert!(halfmove_clock <= 99);
            debug_assert!(self.encountered_boards.len() >= halfmove_clock as usize);

            // Figure out if the new position is repeated (a draw).
            if halfmove_clock >= 4 {
                let boards = &self.encountered_boards;
                let last_irrev = (boards.len() - (halfmove_clock as usize)) as isize;
                unsafe {
                    let mut i = (boards.len() - 4) as isize;
                    while i >= last_irrev {
                        if self.board_hash == *boards.get_unchecked(i as usize) {
                            self.repeated_or_rule50 = true;
                            break;
                        }
                        i -= 2;
                    }
                }
            }

            self.state_stack
                .push(PositionInfo {
                          halfmove_clock: halfmove_clock,
                          last_move: m,
                      });
            return true;
        }

        false
    }

    #[inline]
    fn undo_last_move(&mut self) {
        debug_assert!(self.state_stack.len() > 1);
        unsafe {
            self.position_mut().undo_move(self.state().last_move);
        }
        self.halfmove_count -= 1;
        self.board_hash = self.encountered_boards.pop().unwrap();
        self.repeated_or_rule50 = false;
        self.state_stack.pop();
    }
}


impl<T: Qsearch> Clone for StdSearchNode<T> {
    fn clone(&self) -> Self {
        StdSearchNode {
            position: UnsafeCell::new(self.position().clone()),
            encountered_boards: self.encountered_boards.clone(),
            state_stack: self.state_stack.clone(),
            ..*self
        }
    }
}


impl<T: Qsearch> SetOption for StdSearchNode<T> {
    fn options() -> Vec<(&'static str, OptionDescription)> {
        T::options()
    }

    fn set_option(name: &str, value: &str) {
        T::set_option(name, value)
    }
}


impl<T: Qsearch> StdSearchNode<T> {
    /// Creates a new instance from Forsyth–Edwards Notation (FEN).
    pub fn from_fen(fen: &str) -> Result<StdSearchNode<T>, IllegalBoard> {
        let (board, halfmove_clock, fullmove_number) = try!(parse_fen(fen));
        let gen = try!(T::MoveGenerator::from_board(board));
        Ok(StdSearchNode {
               zobrist: ZobristArrays::get(),
               halfmove_count: ((fullmove_number - 1) << 1) + gen.board().to_move as u16,
               board_hash: gen.hash(),
               position: UnsafeCell::new(gen),
               repeated_or_rule50: false,
               repeated_boards_hash: 0,
               encountered_boards: vec![0; halfmove_clock as usize],
               state_stack: vec![PositionInfo {
                                     halfmove_clock: min(halfmove_clock, 99),
                                     last_move: Move::invalid(),
                                 }],
           })
    }

    /// Forgets the previous playing history, preserves only the set
    /// of previously repeated, still reachable boards.
    fn declare_as_root(&mut self) {
        let state = *self.state();

        // The root position is never deemed as a draw due to
        // repetition or rule-50.
        self.repeated_or_rule50 = false;

        // Calculate the set of previously repeated, still reachable boards.
        let repeated_boards = {
            // Forget all encountered boards before the last irreversible move.
            let last_irrev = self.encountered_boards.len() - state.halfmove_clock as usize;
            self.encountered_boards = self.encountered_boards.split_off(last_irrev);
            self.encountered_boards.reserve(32);

            // Forget all encountered boards that occurred only once.
            set_non_repeated_values(&mut self.encountered_boards, 0)
        };

        // Calculate a collective hash value representing the set of
        // previously repeated, still reachable boards. (We will use
        // this value when calculating position's hash.)
        self.repeated_boards_hash = if repeated_boards.is_empty() {
            0
        } else {
            let mut hasher = DefaultHasher::new();
            for x in repeated_boards {
                hasher.write_u64(x);
            }
            hasher.finish()
        };

        // Forget all played moves.
        self.state_stack = vec![state];
        self.state_stack.reserve(32);
    }

    /// Returns if the root position (the earliest in `state_stack`)
    /// can be reached by playing moves from the current position.
    #[inline]
    fn root_is_reachable(&self) -> bool {
        self.encountered_boards.len() <= self.state().halfmove_clock as usize
    }

    /// Returns if the side to move is checkmated.
    fn is_checkmate(&self) -> bool {
        thread_local!(
            static MOVE_LIST: UnsafeCell<Vec<Move>> = UnsafeCell::new(Vec::new())
        );

        self.is_check() &&
        MOVE_LIST.with(|s| unsafe {
            // Check if there are no legal moves.
            let position = self.position_mut();
            let move_list = &mut *s.get();
            let mut no_legal_moves = true;
            position.generate_all(move_list);
            for m in move_list.iter() {
                if position.do_move(*m).is_some() {
                    position.undo_move(*m);
                    no_legal_moves = false;
                    break;
                }
            }
            move_list.clear();
            no_legal_moves
        })
    }

    #[inline]
    fn state(&self) -> &PositionInfo {
        self.state_stack.last().unwrap()
    }

    #[inline]
    fn position(&self) -> &T::MoveGenerator {
        unsafe { &*self.position.get() }
    }

    #[inline]
    unsafe fn position_mut(&self) -> &mut T::MoveGenerator {
        &mut *self.position.get()
    }
}


/// A helper function. It sets all unique (non-repeated) values in
/// `slice` to `value`, and returns a sorted vector containing a
/// single value for each duplicated value in `slice`.
fn set_non_repeated_values<T>(slice: &mut [T], value: T) -> Vec<T>
    where T: Copy + Ord
{
    let mut repeated = vec![];
    let mut v = slice.to_vec();
    v.sort();
    let mut prev = value;
    for curr in v {
        if curr != value && curr == prev {
            repeated.push(curr);
        }
        prev = curr;
    }
    repeated.dedup();
    for x in slice.iter_mut() {
        if repeated.binary_search(x).is_err() {
            *x = value;
        }
    }
    repeated
}


#[cfg(test)]
mod tests {
    use utils::MoveStack;
    use value::*;
    use search_node::*;
    use evaluator::*;
    use qsearch::*;
    use moves::Move;
    use stock::{StdSearchNode, StdQsearch, StdMoveGenerator, SimpleEvaluator};
    type P = StdSearchNode<StdQsearch<StdMoveGenerator<SimpleEvaluator>>>;

    #[test]
    fn is_legal() {
        assert!(P::from_fen("8/8/8/8/8/8/8/8 w - - 0 1").is_err());
        assert!(P::from_fen("8/8/8/8/8/8/8/7K w - - 0 1").is_err());
        assert!(P::from_fen("k7/8/8/8/8/8/8/7K w - - 0 1").is_ok());
        assert!(P::from_fen("k7/8/8/8/8/8/8/6KK w - - 0 1").is_err());
        assert!(P::from_fen("k7/pppppppp/p7/8/8/8/8/7K w - - 0 1").is_err());
        assert!(P::from_fen("k7/8/8/8/8/7P/PPPPPPPP/7K w - - 0 1").is_err());
        assert!(P::from_fen("k7/pppppppp/8/8/8/8/PPPPPPPP/7K w - - 0 1").is_ok());
        assert!(P::from_fen("k7/1P6/8/8/8/8/8/7K w - - 0 1").is_err());
        assert!(P::from_fen("k7/1B6/8/8/8/8/8/7K w - - 0 1").is_err());
        assert!(P::from_fen("k7/1N6/8/8/8/8/8/7K w - - 0 1").is_ok());
        assert!(P::from_fen("k3P3/8/8/8/8/8/8/7K w - - 0 1").is_err());
        assert!(P::from_fen("k3p3/8/8/8/8/8/8/7K w - - 0 1").is_err());
        assert!(P::from_fen("k7/8/8/8/8/8/8/pP5K w - - 0 1").is_err());
        assert!(P::from_fen("r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1").is_ok());
        assert!(P::from_fen("r3k2r/8/8/8/8/8/8/R3K2B w KQkq - 0 1").is_err());
        assert!(P::from_fen("r3k2r/8/8/8/8/8/8/R3K3 w KQkq - 0 1").is_err());
        assert!(P::from_fen("r3k2r/8/8/8/8/8/8/R3K3 w KQkq - 0 1").is_err());
        assert!(P::from_fen("r3k2r/8/8/8/8/8/8/R3K3 w Qkq - 0 1").is_ok());
        assert!(P::from_fen("r2k3r/8/8/8/8/8/8/R3K3 w Qkq - 0 1").is_err());
        assert!(P::from_fen("r2k3r/8/8/8/8/8/8/R3K3 w Qk - 0 1").is_err());
        assert!(P::from_fen("r2k3r/8/8/8/8/8/8/R3K3 w Q - 0 1").is_ok());
        assert!(P::from_fen("k7/8/8/8/7P/8/8/7K w - h3 0 1").is_err());
        assert!(P::from_fen("k7/8/8/8/7P/8/8/7K b - h3 0 1").is_ok());
        assert!(P::from_fen("k7/8/8/7P/8/8/8/7K b - h4 0 1").is_err());
        assert!(P::from_fen("k7/8/8/8/7P/7P/8/7K b - h3 0 1").is_err());
        assert!(P::from_fen("k7/8/8/8/7P/8/7P/7K b - h3 0 1").is_err());
        assert!(P::from_fen("k7/8/8/8/6P1/7P/8/7K b - h3 0 1").is_err());
        assert!(P::from_fen("8/8/8/6k1/7P/8/8/7K b - h3 0 1").is_ok());
        assert!(P::from_fen("8/8/8/6k1/7P/8/8/6RK b - h3 0 1").is_err());
        assert!(P::from_fen("8/8/8/6k1/3P4/8/8/2B4K b - d3 0 1").is_ok());
        assert!(P::from_fen("8/8/8/6k1/7P/4B3/8/7K b - h3 0 1").is_err());
        assert!(P::from_fen("8/8/8/6k1/7P/8/8/7K b - h3 0 0").is_err());
    }

    #[test]
    fn evaluate_fullmove_number() {
        let mut p = P::from_fen("krq5/p7/8/8/8/8/8/KRQ5 w - - 6 31")
            .ok()
            .unwrap();
        assert_eq!(p.last_move(), Move::invalid());
        assert_eq!(p.fullmove_number(), 31);
        let m = p.legal_moves()[0];
        p.do_move(m);
        assert_eq!(p.last_move(), m);
        assert_eq!(p.fullmove_number(), 31);
        p.undo_last_move();
        assert_eq!(p.fullmove_number(), 31);

        let mut p = P::from_fen("krq5/p7/8/8/8/8/8/KRQ5 b - - 6 31")
            .ok()
            .unwrap();
        assert_eq!(p.fullmove_number(), 31);
        let m = p.legal_moves()[0];
        p.do_move(m);
        assert_eq!(p.fullmove_number(), 32);
        p.undo_last_move();
        assert_eq!(p.fullmove_number(), 31);
    }

    #[test]
    fn evaluate_static() {
        let p = P::from_fen("krq5/p7/8/8/8/8/8/KRQ5 w - - 0 1")
            .ok()
            .unwrap();
        assert!(p.evaluator().evaluate(p.board()) < -20);
    }

    #[test]
    fn evaluate_move() {
        let mut s = MoveStack::new();

        let p = P::from_fen("8/4P1kP/8/8/8/7p/8/7K w - - 0 1")
            .ok()
            .unwrap();
        p.generate_moves(&mut s);
        while let Some(m) = s.pop() {
            if m.notation() == "e7e8q" {
                assert!(p.evaluate_move(m) > 0);
            }
            if m.notation() == "e7e8r" {
                assert!(p.evaluate_move(m) > 0);
            }
            if m.notation() == "h7h8r" {
                assert!(p.evaluate_move(m) < 0);
            }
            if m.notation() == "h1h2" {
                assert_eq!(p.evaluate_move(m), 0);
            }
            if m.notation() == "h1g2" {
                assert!(p.evaluate_move(m) < -5000);
            }
        }

        let p = P::from_fen("6k1/1P6/8/4b3/8/8/8/1R3K2 w - - 0 1")
            .ok()
            .unwrap();
        p.generate_moves(&mut s);
        while let Some(m) = s.pop() {
            if m.notation() == "b7b8q" {
                assert!(p.evaluate_move(m) > 0);
            }
            if m.notation() == "b7b8k" {
                assert!(p.evaluate_move(m) > 0);
            }
        }

        let p = P::from_fen("5r2/8/8/4q1p1/3P4/k3P1P1/P2b1R1B/K4R2 w - - 0 1")
            .ok()
            .unwrap();
        p.generate_moves(&mut s);
        while let Some(m) = s.pop() {
            if m.notation() == "f2f4" {
                assert!(p.evaluate_move(m) <= -400);
            }
            if m.notation() == "e3e4" {
                assert_eq!(p.evaluate_move(m), -100);
            }
            if m.notation() == "g3g4" {
                assert_eq!(p.evaluate_move(m), 0);
            }
            if m.notation() == "f1e1" {
                assert_eq!(p.evaluate_move(m), -500);
            }
            if m.notation() == "f1d1" {
                assert_eq!(p.evaluate_move(m), 0);
            }
        }

        let p = P::from_fen("5r2/8/8/4q1p1/3P4/k3P1P1/P2b1R1B/K4R2 b - - 0 1")
            .ok()
            .unwrap();
        p.generate_moves(&mut s);
        while let Some(m) = s.pop() {
            if m.notation() == "e5e3" {
                assert_eq!(p.evaluate_move(m), 100);
            }
            if m.notation() == "e5d4" {
                assert_eq!(p.evaluate_move(m), -875);
            }
            if m.notation() == "a3a2" {
                assert_eq!(p.evaluate_move(m), -9900);
            }
        }

        let p = P::from_fen("8/8/8/8/8/8/2pkpKp1/8 b - - 0 1")
            .ok()
            .unwrap();
        p.generate_moves(&mut s);
        while let Some(m) = s.pop() {
            if m.notation() == "c2c1r" {
                assert_eq!(p.evaluate_move(m), 400);
            }
            if m.notation() == "c2c1n" {
                assert_eq!(p.evaluate_move(m), 225);
            }
            if m.notation() == "e2e1q" {
                assert_eq!(p.evaluate_move(m), 875);
            }
            if m.notation() == "e2e1n" {
                assert_eq!(p.evaluate_move(m), 225);
            }
            if m.notation() == "g2g1q" {
                assert_eq!(p.evaluate_move(m), -100);
            }
            if m.notation() == "g2g1r" {
                assert_eq!(p.evaluate_move(m), -100);
            }
        }
        assert_eq!(p.evaluate_move(p.null_move()), 0);
    }

    #[test]
    fn repeated_root_position() {
        let moves: Vec<&str> = vec!["g4f3", "g1f1", "f3g4", "f1g1", "g4f3", "g1f1", "f3g4", "f1g1"];
        let p = P::from_history("8/8/8/8/6k1/6P1/8/6K1 b - - 0 1", &mut moves.into_iter())
            .ok()
            .unwrap();
        let mut v = MoveStack::new();
        p.generate_moves(&mut v);
        assert!(v.list().len() != 0);
    }

    #[test]
    fn set_non_repeated_values() {
        use super::set_non_repeated_values;
        let mut v = vec![0, 1, 2, 7, 9, 0, 0, 1, 2];
        let dups = set_non_repeated_values(&mut v, 0);
        assert_eq!(v, vec![0, 1, 2, 0, 0, 0, 0, 1, 2]);
        assert_eq!(dups, vec![1, 2]);
    }

    #[test]
    fn qsearch() {
        let p = P::from_fen("8/8/8/8/8/6qk/7P/7K b - - 0 1")
            .ok()
            .unwrap();
        assert_eq!(p.qsearch(0, -10000, 10000, VALUE_UNKNOWN)
                       .searched_nodes(),
                   1);
    }

    #[test]
    fn is_repeated() {
        let mut p = P::from_fen("8/5p1b/5Pp1/6P1/6p1/3p1pPk/3PpP2/4B2K w - - 0 1")
            .ok()
            .unwrap();
        let mut v = MoveStack::new();
        let mut count = 0;
        for _ in 0..100 {
            p.generate_moves(&mut v);
            while let Some(m) = v.pop() {
                if p.do_move(m) {
                    count += 1;
                    v.clear_all();
                    break;
                }
            }
        }
        assert_eq!(count, 4);
    }

    #[test]
    fn is_checkmate() {
        let p = P::from_fen("8/8/8/8/8/7K/8/5R1k b - - 0 1")
            .ok()
            .unwrap();
        assert!(p.is_checkmate());

        let p = P::from_fen("8/8/8/8/8/7K/6p1/5R1k b - - 0 1")
            .ok()
            .unwrap();
        assert!(!p.is_checkmate());

        let p = P::from_fen("8/8/8/8/8/7K/8/5N1k b - - 0 1")
            .ok()
            .unwrap();
        assert!(!p.is_checkmate());
    }

    #[test]
    fn repeated_boards_hash() {
        let p1 = P::from_fen("8/8/8/8/8/7k/8/7K w - - 0 1").ok().unwrap();
        let moves: Vec<&str> = vec![];
        let p2 = P::from_history("8/8/8/8/8/7k/8/7K w - - 0 1", &mut moves.into_iter())
            .ok()
            .unwrap();
        assert_eq!(p1.board_hash, p2.board_hash);
        assert_eq!(p1.hash(), p2.hash());
        let moves: Vec<&str> = vec!["f1g1", "f3g3", "g1h1", "g3h3"];
        let p2 = P::from_history("8/8/8/8/8/5k2/8/5K2 w - - 0 1", &mut moves.into_iter())
            .ok()
            .unwrap();
        assert_eq!(p1.board_hash, p2.board_hash);
        assert_eq!(p1.hash(), p2.hash());
        let moves: Vec<&str> = vec!["f1g1", "f3g3", "g1f1", "g3f3", "f1g1", "f3g3", "g1h1", "g3h3"];
        let p3 = P::from_history("8/8/8/8/8/5k2/8/5K2 w - - 0 1", &mut moves.into_iter())
            .ok()
            .unwrap();
        assert_eq!(p1.board_hash, p2.board_hash);
        assert!(p1.hash() != p3.hash());
    }
}