The first extra credit (handling dimensions other than 16x16) is
pretty darn easy, so you may want to start out on a 2x2 to get yer
basics working and move up.  With that in mind, here are a few tests
to help verify your work. The test_2x2 tests all possible folding
combinations.

(NOTE: The 16x16 test below is the result of my own solution. I'm
fairly certain it's correct, but not 100%.  So if you run this and
pass the other two tests but fail the 16x16 test, I'd be interested to
see your output and between us figure out what the expected solution
is.)

Oh, and if you have more tests, feel free to share.


require 'test/unit'
require 'test/unit/ui/console/testrunner'

class FoldTest < Test::Unit::TestCase
   def test_2x2
      folds = {"TR" => [4, 2, 1, 3],
               "BR" => [2, 4, 3, 1],
               "TL" => [3, 1, 2, 4],
               "BL" => [1, 3, 4, 2],
               "RT" => [1, 2, 4, 3],
               "RB" => [3, 4, 2, 1],
               "LT" => [2, 1, 3, 4],
               "LB" => [4, 3, 1, 2]}

      folds.each do |cmds,xpct|
         assert_equal xpct, fold(2, 2, cmds)
      end
   end

   def test_16x16
      xpct = [189,  77,  68, 180, 196,  52,  61, 205,
              204,  60,  53, 197, 181,  69,  76, 188,
              185,  73 , 72, 184, 200,  56,  57, 201,
              208,  64,  49, 193, 177,  65,  80, 192,
              191,  79,  66, 178, 194,  50,  63, 207,
              202,  58,  55, 199, 183,  71,  74, 186,
              187,  75,  70, 182, 198,  54,  59, 203,
              206,  62,  51, 195, 179,  67,  78, 190,
              142, 126, 115, 131, 243,   3,  14, 254,
              251,  11,   6, 246, 134, 118, 123, 139,
              138, 122, 119, 135, 247,   7,  10, 250,
              255,  15,   2, 242, 130, 114, 127, 143,
              144, 128, 113, 129, 241,   1,  16, 256,
              249,   9,   8, 248, 136, 120, 121, 137,
              140, 124, 117, 133, 245,   5,  12, 252,
              253,  13,   4, 244, 132, 116, 125, 141,
              157, 109, 100, 148, 228,  20,  29, 237,
              236,  28,  21, 229, 149, 101, 108, 156,
              153, 105, 104, 152, 232,  24,  25, 233,
              240,  32,  17, 225, 145,  97, 112, 160,
              159, 111,  98, 146, 226,  18,  31, 239,
              234,  26,  23, 231, 151, 103, 106, 154,
              155, 107, 102, 150, 230,  22,  27, 235,
              238,  30,  19, 227, 147,  99, 110, 158,
              174,  94,  83, 163, 211,  35,  46, 222,
              219,  43,  38, 214, 166,  86,  91, 171,
              170,  90,  87, 167, 215,  39,  42, 218,
              223,  47,  34, 210, 162,  82,  95, 175,
              176,  96,  81, 161, 209,  33,  48, 224,
              217,  41,  40, 216, 168,  88,  89, 169,
              172,  92,  85, 165, 213,  37,  44, 220,
              221,  45,  36, 212, 164,  84,  93, 173]
      assert_equal xpct, fold(16, 16, "TLBLRRTB")
   end

   def test_invalid
      assert_raise(RuntimeError) { fold(2, 2, "LR") } # too many horz folds
      assert_raise(RuntimeError) { fold(2, 2, "TRB") } # too many folds
      assert_raise(RuntimeError) { fold(3, 2, "LR") } # bad input dimensions
   end

end

Test::Unit::UI::Console::TestRunner.run(FoldTest)