test_puzzle.h

Go to the documentation of this file.
00001 /*
00002  * Sudoku: A plug-in for the Video Disk Recorder
00003  *
00004  * Copyright (C) 2010, Thomas G√ľnther <tom@toms-cafe.de>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License along
00017  * with this program; if not, write to the Free Software Foundation, Inc.,
00018  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019  */
00020 
00021 #include <cxxtest/TestSuite.h>
00022 #include "../puzzle.h"
00023 
00024 using namespace Sudoku;
00025 
00026 class Test_Puzzle : public CxxTest::TestSuite
00027 {
00028 public:
00029   void test_Pos()
00030   {
00031     Pos pos;
00032     TS_ASSERT_EQUALS(pos, Pos::first());
00033     TS_ASSERT_EQUALS(pos, 0u);
00034     TS_ASSERT_EQUALS(pos.col(), 0u);
00035     TS_ASSERT_EQUALS(pos.row(), 0u);
00036     TS_ASSERT_EQUALS(pos.reg(), 0u);
00037     TS_ASSERT_EQUALS(pos, Pos(0, 0));
00038 
00039     TS_ASSERT_EQUALS(pos.prev_col(), pos);
00040     TS_ASSERT_EQUALS(pos.prev_row(), pos);
00041 
00042     TS_ASSERT_EQUALS(pos.next(), Pos(1, 0));
00043     TS_ASSERT_EQUALS(pos.next_col(), Pos(1, 0));
00044     TS_ASSERT_EQUALS(pos.next_row(), Pos(0, 1));
00045 
00046     pos = pos.symmetric();
00047     TS_ASSERT_EQUALS(pos, Pos::last());
00048     TS_ASSERT_EQUALS(pos, 80u);
00049     TS_ASSERT_EQUALS(pos.col(), 8u);
00050     TS_ASSERT_EQUALS(pos.row(), 8u);
00051     TS_ASSERT_EQUALS(pos.reg(), 8u);
00052     TS_ASSERT_EQUALS(pos, Pos(8, 8));
00053 
00054     TS_ASSERT_EQUALS(pos.next(), Pos::last()+1);
00055     TS_ASSERT_EQUALS(pos.next_col(), pos);
00056     TS_ASSERT_EQUALS(pos.next_row(), pos);
00057 
00058     TS_ASSERT_EQUALS(pos.prev_col(), Pos(7, 8));
00059     TS_ASSERT_EQUALS(pos.prev_row(), Pos(8, 7));
00060 
00061     pos = Pos(0, 4);
00062     TS_ASSERT_EQUALS(pos.prev_col(), pos);
00063     pos = Pos(4, 0);
00064     TS_ASSERT_EQUALS(pos.prev_row(), pos);
00065     pos = Pos(4, 8);
00066     TS_ASSERT_EQUALS(pos.next_row(), pos);
00067     pos = Pos(8, 4);
00068     TS_ASSERT_EQUALS(pos.next_col(), pos);
00069     TS_ASSERT_EQUALS(pos.next(), Pos(0, 5));
00070 
00071     pos = Pos::center();
00072     TS_ASSERT_EQUALS(pos, 40u);
00073     TS_ASSERT_EQUALS(pos.col(), 4u);
00074     TS_ASSERT_EQUALS(pos.row(), 4u);
00075     TS_ASSERT_EQUALS(pos.reg(), 4u);
00076     TS_ASSERT_EQUALS(pos, Pos(4, 4));
00077 
00078     pos = pos.next().next_col().next_row();
00079     TS_ASSERT_EQUALS(pos, Pos(6, 5));
00080     TS_ASSERT_EQUALS(pos, 51u);
00081     TS_ASSERT_EQUALS(pos.col(), 6u);
00082     TS_ASSERT_EQUALS(pos.row(), 5u);
00083     TS_ASSERT_EQUALS(pos.reg(), 5u);
00084 
00085     pos = pos.prev_col().prev_row();
00086     TS_ASSERT_EQUALS(pos, Pos(5, 4));
00087 
00088     for (Pos p = Pos::first(); p <= Pos::last(); p = p.next())
00089       if (p == pos)
00090         { TS_ASSERT(!p.interacts_with(pos)); }
00091       else if (p.col() == pos.col())
00092         { TS_ASSERT(p.interacts_with(pos)); }
00093       else if (p.row() == pos.row())
00094         { TS_ASSERT(p.interacts_with(pos)); }
00095       else if (p.reg() == pos.reg())
00096         { TS_ASSERT(p.interacts_with(pos)); }
00097       else
00098         { TS_ASSERT(!p.interacts_with(pos)); }
00099   }
00100 
00101   void test_NumbersEmptyDump()
00102   {
00103     Numbers numbers;
00104     const char dump[] = "_________+"
00105                         "_________+"
00106                         "_________+"
00107                         "_________+"
00108                         "_________+"
00109                         "_________+"
00110                         "_________+"
00111                         "_________+"
00112                         "_________";
00113     TS_ASSERT_SAME_DATA(numbers.get_dump(), dump, sizeof(dump));
00114   }
00115 
00116   void test_NumbersConstruct()
00117   {
00118     const char dump[] = "________9+"
00119                         "_______8_+"
00120                         "______7__+"
00121                         "_____6___+"
00122                         "____5____+"
00123                         "___4_____+"
00124                         "__3______+"
00125                         "_2_______+"
00126                         "1________";
00127     Numbers numbers(dump);
00128     for (Pos p = Pos::first(); p <= Pos::last(); p = p.next())
00129       if (p.col() == DIM - 1 - p.row())
00130         { TS_ASSERT_EQUALS(numbers.get(p), p.col()+1); }
00131       else
00132         { TS_ASSERT_EQUALS(numbers.get(p), 0u); }
00133     TS_ASSERT_SAME_DATA(numbers.get_dump(), dump, sizeof(dump));
00134   }
00135 
00136   void test_NumbersSetGet()
00137   {
00138     Numbers numbers;
00139     numbers.set(Pos(0, 0), 1);
00140     numbers.set(Pos(1, 1), 2);
00141     numbers.set(Pos(2, 2), 3);
00142     numbers.set(Pos(3, 3), 4);
00143     numbers.set(Pos(4, 4), 5);
00144     numbers.set(Pos(5, 5), 6);
00145     numbers.set(Pos(6, 6), 7);
00146     numbers.set(Pos(7, 7), 8);
00147     numbers.set(Pos(8, 8), 9);
00148     for (Pos p = Pos::first(); p <= Pos::last(); p = p.next())
00149       if (p.col() == p.row())
00150         { TS_ASSERT_EQUALS(numbers.get(p), p.col()+1); }
00151       else
00152         { TS_ASSERT_EQUALS(numbers.get(p), 0u); }
00153   }
00154 
00155   void test_PuzzleEmpty()
00156   {
00157     Puzzle puzzle;
00158     for (Pos p = Pos::first(); p <= Pos::last(); p = p.next())
00159     {
00160       TS_ASSERT_EQUALS(puzzle.get(p), 0u);
00161       TS_ASSERT(!puzzle.given(p));
00162     }
00163   }
00164 
00165   void test_PuzzleNumbers()
00166   {
00167     const char dump[] = "________9+"
00168                         "_______8_+"
00169                         "______7__+"
00170                         "_____6___+"
00171                         "____5____+"
00172                         "___4_____+"
00173                         "__3______+"
00174                         "_2_______+"
00175                         "1________";
00176     Numbers numbers(dump);
00177     Puzzle puzzle(dump);
00178     for (Pos p = Pos::first(); p <= Pos::last(); p = p.next())
00179     {
00180       TS_ASSERT_EQUALS(puzzle.get(p), numbers.get(p));
00181       TS_ASSERT_EQUALS(puzzle.given(p), numbers.get(p) != 0u);
00182     }
00183   }
00184 };
Generated on Mon Apr 5 17:01:07 2010 for VDR plugin 'Sudoku' by  doxygen 1.6.3