summaryrefslogtreecommitdiff
path: root/src/script.c
blob: 532a0d50cf807750820913e16c4c6d075f713b28 (plain)
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
/**

Copyright (c) Scott Gasch

Module Name:

    script.c

Abstract:

    Some glue code to support running test scripts and keeping track
    of test suite results.

Author:

    Scott Gasch ([email protected]) 18 May 2004

Revision History:

    $Id: script.c 355 2008-07-01 15:46:43Z scott $

**/

#include "chess.h"

// Declared directly instead of #include <math.h> -- that header #defines
// its own INFINITY_SCORE, which collides with ours (chess.h:198, MAX_SHORT) and
// would silently change what INFINITY_SCORE means for the rest of this file.
extern double pow(double, double);

// ----------------------------------------------------------------------
//
// Global testsuite position counters
// 
#define SUITE_MAX_SOLUTIONS                   (4)
#define SUITE_MAX_AVOIDS                      (4)
#define SUITE_NUM_HISTOGRAM                   (8)

//
// A big struct of counters we want to maintain when running test scripts.
//
typedef struct _SUITE_COUNTERS
{
    CHAR szId[SMALL_STRING_LEN_CHAR];
    ULONG uNumSolutions;
    MOVE mvSolution[SUITE_MAX_SOLUTIONS];
    ULONG uNumAvoids;
    MOVE mvAvoid[SUITE_MAX_AVOIDS];
    ULONG uCorrect;
    ULONG uIncorrect;
    ULONG uTotal;
    UINT64 u64TotalNodeCount;
    double dCurrentSolutionTime;
    ULONG uCurrentSolvedPlies;
    ULONG uSigmaDepth;
    double dSigmaSolutionTime;
    double dAverageNps;
    UINT64 u64TotalBetaCutoffs;
    UINT64 u64TotalBetaCutoffsOnFirstMove;
    UINT64 u64TotalCounterMoveTries;
    UINT64 u64TotalCounterMoveHits;
    UINT64 u64TotalLeftoverTries;
    UINT64 u64TotalLeftoverAlpha;
    UINT64 u64TotalLeftoverFH;
    UINT64 u64TotalCMEvidenceTries[CM_EVIDENCE_BUCKETS];
    UINT64 u64TotalCMEvidenceFH[CM_EVIDENCE_BUCKETS];
    double dSigmaEBF;             // sum of per-problem nodes^(1/depth)
    ULONG uEBFCount;              // number of problems with depth > 0
    double dAverageTimeToSolution;
    ULONG uHistogram[SUITE_NUM_HISTOGRAM];
}
SUITE_COUNTERS;
static SUITE_COUNTERS g_SuiteCounters;


static void 
_ClearSuiteData(void)
/**

Routine description:

    Clear data/counters we have for the current test script.

Parameters:

    void

Return value:

    static void

**/
{
    g_SuiteCounters.uNumSolutions = 0;
    memset(g_SuiteCounters.mvSolution,
           0,
           sizeof(g_SuiteCounters.mvSolution));
    g_SuiteCounters.uNumAvoids = 0;
    memset(g_SuiteCounters.mvAvoid,
           0,
           sizeof(g_SuiteCounters.mvAvoid));
    memset(g_SuiteCounters.szId,
           0,
           sizeof(g_SuiteCounters.szId));
    g_SuiteCounters.uCurrentSolvedPlies = 0;
    g_SuiteCounters.dCurrentSolutionTime = 0;
}


COMMAND(IdCommand)
/**

Routine description:

    This function implements the 'id' engine command.  

    Usage:
    
        id [optional string]

        With no agruments, id returns the current position's id tag.
        With a string argument, id sets the current position's id tag.

        id tags are useful[?] for naming positions in test suites...

Parameters:

    The COMMAND macro hides four arguments from the input parser:

        CHAR *szInput : the full line of input
        ULONG argc    : number of argument chunks
        CHAR *argv[]  : array of ptrs to each argument chunk
        POSITION *pos : a POSITION pointer to operate on

Return value:

    void

**/
{
    CHAR *p;
    ULONG u;

    if (argc < 2)
    {
        Trace("The current position's id is: \"%s\"\n",
              g_SuiteCounters.szId);
    }
    else
    {
        p = szInput;
        while(*p && !isspace(*p))
        {
            p++;
        }
        while(*p && isspace(*p))
        {
            p++;
        }
        u = 0;
        while(*p)
        {
            if (!isspace(*p))
            {
                g_SuiteCounters.szId[u] = *p;
                u++;
            }
            if (u > ARRAY_LENGTH(g_SuiteCounters.szId))
            {
                break;
            }
            p++;
        }
    }
}

COMMAND(SolutionCommand)
/**

Routine description:

    This function implements the 'solution' engine command.  

    Usage:
    
        solution [optional move string]

        With no agruments, solution prints out a list of the "correct
        solution" move(s) set for the current position.

        With a move string argument solution sets another move as the
        correct answer to the current position.  The move string can
        be in ICS or SAN format.

        When the engine is allowed to search a position it will
        compare its best move from the search against the solution
        move(s) for the position and keep track of how many it gets
        right/wrong and how long it took to solve.

        A position can have up to three solutions.  A position cannot
        have both solution moves and avoid moves.

Parameters:

    The COMMAND macro hides four arguments from the input parser:

        CHAR *szInput : the full line of input
        ULONG argc    : number of argument chunks
        CHAR *argv[]  : array of ptrs to each argument chunk
        POSITION *pos : a POSITION pointer to operate on

Return value:

    void

**/
{
    ULONG u;
    MOVE mv;

    if (argc < 2)
    {
        Trace("There %s %u solution move%s set:\n",
              (g_SuiteCounters.uNumSolutions > 1) ? "are" : "is",
              g_SuiteCounters.uNumSolutions,
              (g_SuiteCounters.uNumSolutions > 1) ? "s" : "");
        for(u = 0;
            u < g_SuiteCounters.uNumSolutions;
            u++)
        {
            Trace("    %s\n", MoveToSan(g_SuiteCounters.mvSolution[u], pos));
        }
    }
    else
    {
        mv.uMove = 0;
        switch(LooksLikeMove(argv[1]))
        {
            case MOVE_SAN:
                mv = ParseMoveSan(argv[1], pos);
                break;
            case MOVE_ICS:
                mv = ParseMoveIcs(argv[1], pos);
                break;
            case NOT_MOVE:
                break;
        }

        if (mv.uMove == 0)
        {
            Trace("Error (bad move): %s\n", argv[1]);
            return;
        }
        
        if (g_SuiteCounters.uNumSolutions < SUITE_MAX_SOLUTIONS)
        {
            g_SuiteCounters.mvSolution[g_SuiteCounters.uNumSolutions] = mv;
            g_SuiteCounters.uNumSolutions++;
        }
    }
}


COMMAND(AvoidCommand)
/**

Routine description:

    This function implements the 'avoid' engine command.  

    Usage:
    
        avoid [optional move string]

        With no agruments, avoid prints out a list of the "don't play
        this" move(s) set for the current position.

        With a move string argument, avoid sets another move as the
        incorrect answer to the current position.  The move string can
        be in ICS or SAN format.

        When the engine is allowed to search a position it will
        compare its best move from the search against the avoid
        move(s) for the position and keep track of how many it gets
        right/wrong and how long it took to solve.

        A position can have up to three avoid moves.  A position
        cannot have both solution moves and avoid moves.

Parameters:

    The COMMAND macro hides four arguments from the input parser:

        CHAR *szInput : the full line of input
        ULONG argc    : number of argument chunks
        CHAR *argv[]  : array of ptrs to each argument chunk
        POSITION *pos : a POSITION pointer to operate on

Return value:

    void

**/
{
    ULONG u;
    MOVE mv;

    if (argc < 2)
    {
        Trace("There %s %u avoid move%s set:\n",
              (g_SuiteCounters.uNumAvoids > 1) ? "are" : "is",
              g_SuiteCounters.uNumAvoids,
              (g_SuiteCounters.uNumAvoids > 1) ? "s" : "");
        for(u = 0;
            u < g_SuiteCounters.uNumAvoids;
            u++)
        {
            Trace("    %s\n", MoveToSan(g_SuiteCounters.mvAvoid[u], pos));
        }
    }
    else
    {
        mv.uMove = 0;
        switch(LooksLikeMove(argv[1]))
        {
            case MOVE_SAN:
                mv = ParseMoveSan(argv[1], pos);
                break;
            case MOVE_ICS:
                mv = ParseMoveIcs(argv[1], pos);
                break;
            case NOT_MOVE:
                break;
        }

        if (mv.uMove == 0)
        {
            Trace("Error (bad move): %s\n", argv[1]);
            return;
        }
        
        if (g_SuiteCounters.uNumAvoids < SUITE_MAX_AVOIDS)
        {
            g_SuiteCounters.mvAvoid[g_SuiteCounters.uNumAvoids] = mv;
            g_SuiteCounters.uNumAvoids++;
        }
    }
}



//
// Shared by ScriptCommand and EpdCommand: process one already-read
// line exactly the way a script file's line is processed -- "go"
// triggers a search plus counter accumulation, anything else is
// dispatched through the normal command parser. Factored out so
// EpdCommand can feed it lines synthesized from a raw EPD record
// (setboard/id/solution/avoid/go) instead of lines read verbatim from
// a file, without duplicating this logic or its counter bookkeeping.
//
static void
_ProcessSuiteLine(char *szLine, POSITION *pos)
{
    Trace("SCRIPT> %s\n", szLine);
    if (!STRNCMPI(szLine, "go", 2))
    {
        (void)Think(pos);
        g_SuiteCounters.u64TotalNodeCount +=
            g_Options.u64NodesSearched;
        g_SuiteCounters.u64TotalBetaCutoffs +=
            g_Options.u64BetaCutoffs;
        g_SuiteCounters.u64TotalBetaCutoffsOnFirstMove +=
            g_Options.u64BetaCutoffsOnFirstMove;
        g_SuiteCounters.u64TotalCounterMoveTries +=
            g_Options.u64CounterMoveTries;
        g_SuiteCounters.u64TotalCounterMoveHits +=
            g_Options.u64CounterMoveHits;
        g_SuiteCounters.u64TotalLeftoverTries +=
            g_Options.u64LeftoverTries;
        g_SuiteCounters.u64TotalLeftoverAlpha +=
            g_Options.u64LeftoverAlpha;
        g_SuiteCounters.u64TotalLeftoverFH +=
            g_Options.u64LeftoverFH;
        {
            ULONG v;
            for (v = 0; v < CM_EVIDENCE_BUCKETS; v++)
            {
                g_SuiteCounters.u64TotalCMEvidenceTries[v] +=
                    g_Options.u64CMEvidenceTries[v];
                g_SuiteCounters.u64TotalCMEvidenceFH[v] +=
                    g_Options.u64CMEvidenceFH[v];
            }
        }
    }
    else
    {
        ExecuteCommandLineDirectly(szLine, pos);
    }
}


//
// Shared by ScriptCommand and EpdCommand: the end-of-suite banner and
// statistics report. Verbatim extraction of what used to be
// ScriptCommand's own tail -- see that function's history for the
// meaning of each field.
//
static void
_PrintSuiteReport(double dSuiteStart)
{
    ULONG u, v, uMax;
    double dMult = (double)g_Options.uMyIncrement / (double)SUITE_NUM_HISTOGRAM;

    Banner();
    Trace("\n"
          "TEST SCRIPT execution complete.  Final statistics:\n"
          "--------------------------------------------------\n"
          "    correct solutions   : %u\n"
          "    incorrect solutions : %u\n"
          "    total problems      : %u\n"
          "    total nodecount     : %"
               COMPILER_LONGLONG_UNSIGNED_FORMAT "\n"
          "    avg. search speed   : %6.1f nps\n"
          "    avg. solution time  : %3.1f sec\n"
          "    avg. search depth   : %4.1f ply\n"
          "    1st move beta cut   : %5.2f percent\n"
          "    avg. eff. branching : %5.3f\n"
          "    counter move hit %% : %5.2f percent (%"
               COMPILER_LONGLONG_UNSIGNED_FORMAT " tries)\n"
          "    leftover            : %5.2f%% alpha, %5.2f%% FH (%"
               COMPILER_LONGLONG_UNSIGNED_FORMAT " tries)\n"
          "    script time         : %6.1f sec\n\n",
          g_SuiteCounters.uCorrect,
          g_SuiteCounters.uIncorrect,
          g_SuiteCounters.uTotal,
          g_SuiteCounters.u64TotalNodeCount,
          ((double)g_SuiteCounters.u64TotalNodeCount /
           (SystemTimeStamp() - dSuiteStart)),
          ((g_SuiteCounters.uCorrect > 0) ?
           (g_SuiteCounters.dSigmaSolutionTime /
            (double)g_SuiteCounters.uCorrect) : 0.0),
          ((double)g_SuiteCounters.uSigmaDepth /
           (double)g_SuiteCounters.uTotal),
          (100.0 * (double)g_SuiteCounters.u64TotalBetaCutoffsOnFirstMove /
           ((double)g_SuiteCounters.u64TotalBetaCutoffs + 1.0)),
          (g_SuiteCounters.dSigmaEBF /
           ((double)g_SuiteCounters.uEBFCount + 1.0)),
          (100.0 * (double)g_SuiteCounters.u64TotalCounterMoveHits /
           ((double)g_SuiteCounters.u64TotalCounterMoveTries + 1.0)),
          g_SuiteCounters.u64TotalCounterMoveTries,
          (100.0 * (double)g_SuiteCounters.u64TotalLeftoverAlpha /
           ((double)g_SuiteCounters.u64TotalLeftoverTries + 1.0)),
          (100.0 * (double)g_SuiteCounters.u64TotalLeftoverFH /
           ((double)g_SuiteCounters.u64TotalLeftoverTries + 1.0)),
          g_SuiteCounters.u64TotalLeftoverTries,
          (SystemTimeStamp() - dSuiteStart));

    // Countermove evidence calibration -- FH% per evidence bucket,
    // an ongoing check that COUNTERMOVE_EVIDENCE_THRESHOLD (chess.h)
    // is still well-calibrated as the engine/data evolve.
    {
        static const ULONG uCMEvidenceFloors[CM_EVIDENCE_BUCKETS] =
            { 0, 1, 100, 1000, 10000, 100000, 1000000 };
        Trace("\ncountermove evidence calibration:\n");
        for (v = 0; v < CM_EVIDENCE_BUCKETS; v++)
        {
            Trace("    evidence >= %8lu : %5.2f%% FH (%"
                  COMPILER_LONGLONG_UNSIGNED_FORMAT " tries)\n",
                  uCMEvidenceFloors[v],
                  (100.0 * (double)g_SuiteCounters.u64TotalCMEvidenceFH[v] /
                   ((double)g_SuiteCounters.u64TotalCMEvidenceTries[v] + 1.0)),
                  g_SuiteCounters.u64TotalCMEvidenceTries[v]);
        }
    }

    // Histogram stuff
    if (g_SuiteCounters.uTotal > 0) {
        uMax = g_SuiteCounters.uHistogram[0];
        for (u = 1; u < SUITE_NUM_HISTOGRAM; u++)
        {
            if (g_SuiteCounters.uHistogram[u] > uMax)
            {
                uMax = g_SuiteCounters.uHistogram[u];
            }
        }
        for (u = 0; u < SUITE_NUM_HISTOGRAM; u++)
        {
            Trace("%4.1f .. %4.1f: ",
                  (double)u * dMult, (double)(u + 1) * dMult);
            for (v = 0;
                 (uMax > 0) && (v < 50 * g_SuiteCounters.uHistogram[u] / uMax);
                 v++)
            {
                Trace("*");
            }
            Trace("\n");
        }
    }
}


COMMAND(ScriptCommand)
/**

Routine description:

    This function implements the 'script' engine command.  

    Usage:
    
        script <required filename>

        The script command opens the reads the required filename and
        reads it line by line.  Each line of the script file is
        treated as a command and executed in sequence.  Each command
        is executed fully before the next script line is read and
        executed.  When all lines in the script file have been read
        and executed the engine will print out a post-script report of
        test suite statistics and then begin to pay attention to
        commands from the keyboard again.

        Scripts are meant to facilitate execution of test suites.

Parameters:

    The COMMAND macro hides four arguments from the input parser:

        CHAR *szInput : the full line of input
        ULONG argc    : number of argument chunks
        CHAR *argv[]  : array of ptrs to each argument chunk
        POSITION *pos : a POSITION pointer to operate on

Return value:

    void

**/
{
    FILE *p;
    static CHAR szLine[SMALL_STRING_LEN_CHAR];
    double dSuiteStart;

    if (argc < 2)
    {
        Trace("Error (Usage: script <filename>): %s\n", argv[0]);
        return;
    }
    if (FALSE == SystemDoesFileExist(argv[1]))
    {
        Trace("Error (file doesn't exist): %s\n", argv[1]);
        return;
    }

    p = fopen(argv[1], "rb");
    if (NULL != p)
    {
        dSuiteStart = SystemTimeStamp();
        memset(&(g_SuiteCounters), 0, sizeof(g_SuiteCounters));
        while(fgets(szLine, ARRAY_LENGTH(szLine), p))
        {
            _ProcessSuiteLine(szLine, pos);
            if (TRUE == g_fExitProgram) break;
        }
        fclose(p);
        _PrintSuiteReport(dSuiteStart);
    }
}


COMMAND(EpdCommand)
/**

Routine description:

    Runs a raw EPD test suite file directly -- no perl preprocessing
    into a setboard/solution/go script first. Historically the only
    way to run a suite from real-world EPD (the "board stm castle ep
    [halfmove fullmove] bm <move>; id \"...\"; ..." format most public
    tactical suites ship in) was tests/convert_epd.pl, which had a
    real, silent-corruption bug: its single "everything between the
    board and bm is [^b]+" regex breaks on any Black-to-move record
    (the side-to-move field is literally the character "b"), and
    because $fen/$id/$solution were unscoped globals rather than
    per-line locals, a failed match silently carried the *previous*
    record's board and solution forward instead of erroring --
    confirmed live by feeding it a real 5-position batch, where the
    one Black-to-move line came out as a byte-for-byte duplicate of
    the line before it. This command replaces that whole external
    preprocessing step with real per-record EPD parsing (see
    ParseEpdRecord, fen.c) feeding the exact same
    _ProcessSuiteLine/_PrintSuiteReport machinery ScriptCommand uses,
    so a raw EPD file can just be handed to the engine directly.

    Usage:

        epd <required filename>

Parameters:

    The COMMAND macro hides four arguments from the input parser:

        CHAR *szInput : the full line of input
        ULONG argc    : number of argument chunks
        CHAR *argv[]  : array of ptrs to each argument chunk
        POSITION *pos : a POSITION pointer to operate on

Return value:

    void

**/
{
    FILE *p;
    static CHAR szLine[SMALL_STRING_LEN_CHAR];
    static CHAR szSynth[SMALL_STRING_LEN_CHAR];
    CHAR szFen[SMALL_STRING_LEN_CHAR];
    CHAR szId[SMALL_STRING_LEN_CHAR];
    CHAR szMoves[SUITE_MAX_SOLUTIONS + SUITE_MAX_AVOIDS][SMALL_STRING_LEN_CHAR];
    ULONG uNumMoves;
    FLAG fIsAvoid;
    double dSuiteStart;
    ULONG u;
    ULONG uLineNum = 0;

    if (argc < 2)
    {
        Trace("Error (Usage: epd <filename>): %s\n", argv[0]);
        return;
    }
    if (FALSE == SystemDoesFileExist(argv[1]))
    {
        Trace("Error (file doesn't exist): %s\n", argv[1]);
        return;
    }

    p = fopen(argv[1], "rb");
    if (NULL != p)
    {
        dSuiteStart = SystemTimeStamp();
        memset(&(g_SuiteCounters), 0, sizeof(g_SuiteCounters));
        while(fgets(szLine, ARRAY_LENGTH(szLine), p))
        {
            uLineNum++;
            if (FALSE == ParseEpdRecord(szLine, szFen, ARRAY_LENGTH(szFen),
                                        szId, ARRAY_LENGTH(szId),
                                        szMoves, ARRAY_LENGTH(szMoves),
                                        &uNumMoves, &fIsAvoid))
            {
                if (szLine[0] && (szLine[0] != '\n') && (szLine[0] != '#'))
                {
                    Trace("epd: skipping unparseable line %lu: %s",
                          uLineNum, szLine);
                }
                continue;
            }

            snprintf(szSynth, ARRAY_LENGTH(szSynth), "setboard %s\n", szFen);
            _ProcessSuiteLine(szSynth, pos);
            if (TRUE == g_fExitProgram) break;

            if (szId[0])
            {
                snprintf(szSynth, ARRAY_LENGTH(szSynth), "id %s\n", szId);
                _ProcessSuiteLine(szSynth, pos);
            }

            for (u = 0; u < uNumMoves; u++)
            {
                snprintf(szSynth, ARRAY_LENGTH(szSynth), "%s %s\n",
                          fIsAvoid ? "avoid" : "solution", szMoves[u]);
                _ProcessSuiteLine(szSynth, pos);
            }

            _ProcessSuiteLine("go\n", pos);
            if (TRUE == g_fExitProgram) break;
        }
        fclose(p);
        _PrintSuiteReport(dSuiteStart);
    }
}


FLAG
WeAreRunningASuite(void)
/**

Routine description:

    A function that returns TRUE if the engine is running a test suite
    and FALSE otherwise.

Parameters:

    void

Return value:

    FLAG

**/
{
    if (g_SuiteCounters.uNumSolutions +
        g_SuiteCounters.uNumAvoids)
    {
        return(TRUE);
    }
    return(FALSE);
}


FLAG 
CheckTestSuiteMove(MOVE mv, SCORE iScore, ULONG uDepth)
/**

Routine description:

    This function is called by RootSearch when it has finished a
    search.  The MOVE parameter is the best move that the search
    found.  This code checks that move against the solution or avoid
    moves for the current position, determines if we "got it right"
    and updates the test suite counters accordingly.

Parameters:

    MOVE mv

Return value:

    FLAG

**/
{
    FLAG fSolved = FALSE;
    ULONG u;

    if (!WeAreRunningASuite()) return FALSE;

    //
    // There is(are) solution move(s).
    //
    if (g_SuiteCounters.uNumSolutions)
    {
        ASSERT(g_SuiteCounters.uNumAvoids == 0);
        fSolved = FALSE;
        for (u = 0; u < g_SuiteCounters.uNumSolutions; u++)
        {
            if ((mv.cFrom == g_SuiteCounters.mvSolution[u].cFrom) &&
                (mv.cTo == g_SuiteCounters.mvSolution[u].cTo))
            {
                fSolved = TRUE;
                break;
            }
        }
        if (!fSolved) {
            g_SuiteCounters.uCurrentSolvedPlies = 0;
            g_SuiteCounters.dCurrentSolutionTime = 0.0;
        } else {
            if (g_SuiteCounters.uCurrentSolvedPlies == 0) {
                g_SuiteCounters.dCurrentSolutionTime = 
                    (SystemTimeStamp() - g_MoveTimer.dStartTime);
            }
            g_SuiteCounters.uCurrentSolvedPlies++;
        }
    }
    else if (g_SuiteCounters.uNumAvoids) 
    {
        //
        // Handle avoid-move problems.
        //
        fSolved = TRUE;
        for (u = 0; u < g_SuiteCounters.uNumAvoids; u++)
        {
            if ((mv.cFrom == g_SuiteCounters.mvAvoid[u].cFrom) &&
                (mv.cTo == g_SuiteCounters.mvAvoid[u].cTo))
            {
                fSolved = FALSE;
            }
        }
        if (!fSolved) 
        {
            g_SuiteCounters.uCurrentSolvedPlies = 0;
            g_SuiteCounters.dCurrentSolutionTime = 0.0;
        } else {
            if (g_SuiteCounters.uCurrentSolvedPlies == 0) {
                g_SuiteCounters.dCurrentSolutionTime = 
                    (SystemTimeStamp() - g_MoveTimer.dStartTime);
            }
            g_SuiteCounters.uCurrentSolvedPlies++;
        }
    }
    else
    {
        UtilPanic(SHOULD_NOT_GET_HERE,
                  NULL, NULL, NULL, NULL,
                  __FILE__, __LINE__);
    }

    //
    // Handle "FastScript" variable for solution moves.
    //
    if ((g_Options.fFastScript) &&
        (g_SuiteCounters.uCurrentSolvedPlies > 4) &&
        (iScore >= 0))
    {
        if (((g_Options.uMyIncrement != 0) &&
             (SystemTimeStamp() - g_MoveTimer.dStartTime >=
              (double)g_Options.uMyIncrement / 3)) ||
            ((g_Options.uMaxDepth != 0) &&
             (uDepth >= g_Options.uMaxDepth / 3)))
        {
            Trace("FAST SCRIPT --> stop searching now\n");
            g_MoveTimer.bvFlags |= TIMER_STOPPING;
        }
    }
    return(fSolved);
}


void 
PostMoveTestSuiteReport(SEARCHER_THREAD_CONTEXT *ctx)
/**

Routine description:

    If we're running a suite, print a little blurb after each move to
    act as a running total of how we're doing.

Parameters:

    SEARCHER_THREAD_CONTEXT *ctx

Return value:

    void

**/
{
    MOVE mv = ctx->mvRootMove;
    ULONG uDepth = ctx->uRootDepth / ONE_PLY, y;
    double dMult;

    //
    // This is the real move the searched picked when all was said and
    // done (i.e. it ran out of time or reached a fixed depth).  See
    // if it's right.
    //
    if (WeAreRunningASuite())
    {
        g_SuiteCounters.uTotal++;
        g_SuiteCounters.uSigmaDepth += uDepth;
        if (uDepth > 0)
        {
            g_SuiteCounters.dSigmaEBF +=
                pow((double)ctx->sCounters.tree.u64TotalNodeCount,
                    1.0 / (double)uDepth);
            g_SuiteCounters.uEBFCount++;
        }

        if (CheckTestSuiteMove(mv, -1, uDepth))
        {
            Trace("Problem %s solved in %3.1f sec.\n", 
                  g_SuiteCounters.szId,
                  g_SuiteCounters.dCurrentSolutionTime);
            g_SuiteCounters.uCorrect++;
            g_SuiteCounters.dSigmaSolutionTime += 
                g_SuiteCounters.dCurrentSolutionTime;

            dMult = (double)g_Options.uMyIncrement / 
                (double)SUITE_NUM_HISTOGRAM;
            ASSERT(SUITE_NUM_HISTOGRAM > 0);
            for (y = 1; y <= SUITE_NUM_HISTOGRAM; y++)
            {
                if (g_SuiteCounters.dCurrentSolutionTime < ((double)y * dMult))
                {
                    g_SuiteCounters.uHistogram[y-1]++;
                    break;
                }
                if (y == SUITE_NUM_HISTOGRAM)
                {
                    //
                    // This one is actually > time-to-think.  This
                    // really happens... like we had 20 sec to think
                    // and solved it in 20.1 or something before the
                    // stop-searching timer was checked.
                    //
                    g_SuiteCounters.uHistogram[y-1]++;
                }
            }
        }
        else
        {
            Trace(">>>>> Problem %s was not solved. <<<<<\n\n",
                  g_SuiteCounters.szId);
            g_SuiteCounters.uIncorrect++;
        }
        Trace("Current suite score: %u correct of %u problems.\n", 
              g_SuiteCounters.uCorrect,
              g_SuiteCounters.uTotal);
        _ClearSuiteData();
    }
}