add_library(lat_neigh neigh.cpp)
 
 add_library(lat_writeout writeout.cpp)
+
+add_executable(neigh_test neigh_test.cpp)
+target_link_libraries(neigh_test lat_neigh)
\ No newline at end of file
 
--- /dev/null
+#include "neigh.h"
+#include <time.h>
+#include <stdlib.h>
+#include <iostream>
+
+using namespace std;
+
+int main()
+{
+  neigh *n;
+  srand(time(NULL));
+
+  while( true )
+    {
+      int l[] = { rand()%10 + 1, rand()%10 + 1, rand()%10 + 1 , rand()%10 + 1 };
+
+      n = new neigh(4,l[0],l[1],l[2],l[3]);
+
+      cout << l[0] << "x" << l[1] << "x" << l[2] << "x" << l[3] << endl; 
+
+      for(int itry=0; itry<100000; itry++)
+       {
+         int dir0 = rand()%4;
+         int dir1 = rand()%3;
+         if(dir0 <= dir1) dir1++;
+
+         int l0 = rand()%(3*l[dir0]);
+         int l1 = rand()%(3*l[dir1]);
+
+         int startpos = rand()%l[0] + (rand()%l[1])*l[0] + (rand()%l[2])*l[0]*l[1] + (rand()%l[3])*l[0]*l[1]*l[2];
+
+         int pos = startpos;
+
+         for(int istep=0; istep<l0; istep++) pos = (*n)[pos*8+dir0];     
+         for(int istep=0; istep<l1; istep++) pos = (*n)[pos*8+dir1];
+         for(int istep=0; istep<l0; istep++) pos = (*n)[pos*8+dir0+4];
+         for(int istep=0; istep<l1; istep++) pos = (*n)[pos*8+dir1+4];
+
+         if( pos != startpos ) {
+           cout << "OHOH!!!" << endl;
+           exit(1);
+         }
+       }
+
+      delete n;
+    }
+
+  return 0;
+}