Index: /library/bdm/base/bdmbase.h
===================================================================
--- /library/bdm/base/bdmbase.h (revision 432)
+++ /library/bdm/base/bdmbase.h (revision 436)
@@ -197,4 +197,7 @@
 
   // TODO dodelat void to_setting( Setting &set ) const;
+
+  //! Invalidate all named RVs. Use before initializing any RV instances, with care...
+  static void clear_all();
 };
 UIREGISTER(RV);
Index: /library/bdm/base/bdmbase.cpp
===================================================================
--- /library/bdm/base/bdmbase.cpp (revision 430)
+++ /library/bdm/base/bdmbase.cpp (revision 436)
@@ -12,4 +12,11 @@
 RV RV0=RV();
 
+void RV::clear_all()
+{
+    RV_MAP.clear();
+    RV_SIZES.clear();
+    RV_NAMES = Array<string>(RV_BUFFER_STEP);
+}
+  
 int RV::init ( const string &name, int size ) {
 	//Refer
Index: /library/tests/epdf_harness.h
===================================================================
--- /library/tests/epdf_harness.h (revision 436)
+++ /library/tests/epdf_harness.h (revision 436)
@@ -0,0 +1,48 @@
+/*!
+  \file
+  \brief Support for testing descendants of epdf.
+  \author Vaclav Barta.
+
+  -----------------------------------
+  BDM++ - C++ library for Bayesian Decision Making under Uncertainty
+
+  Using IT++ for numerical operations
+  -----------------------------------
+*/
+
+#ifndef EPDF_HARNESS_H
+#define EPDF_HARNESS_H
+
+#define BDMLIB // not an ideal way to prevent double registration of UI factories...
+
+#include "bdmroot.h"
+#include "itpp_ext.h"
+#include "shared_ptr.h"
+#include "base/bdmbase.h"
+#include "base/user_info.h"
+
+namespace bdm
+{
+
+class epdf_harness : public root
+{
+private:
+    shared_ptr<epdf> hepdf;
+    vec mean;
+    vec variance;
+    double lognc;
+    mat support;
+    int nsamples;
+    double integral;
+
+public:
+    epdf_harness():lognc(0), nsamples(100), integral(0) { }
+
+    void test();
+
+    void from_setting(const Setting &set);
+};
+
+}
+
+#endif
Index: /library/tests/egiw.cfg
===================================================================
--- /library/tests/egiw.cfg (revision 436)
+++ /library/tests/egiw.cfg (revision 436)
@@ -0,0 +1,22 @@
+data = (
+{
+  class = "epdf_harness";
+  epdf = {
+    class = "egiw";
+    V = ( "matrix", 2, 2, [ 12.2, 11.0, 11.0, 10.0 ] );
+    nu = 10;
+    dimx = 1.0;
+    rv = {
+      class = "RV";
+      names = ( "a", "b" );
+    };
+  };
+  mean = [ 1.1, 0.02 ];
+  lognc = 11.45368;
+  variance = [ 0.002, 8e-05 ];
+  support = ( "matrix", 2, 2, [ -2.0, 4.0, 0.01, 2.0 ] );
+  nsamples = 200;
+  integral = 1.0;
+} );
+
+
Index: /library/tests/test_util.h
===================================================================
--- /library/tests/test_util.h (revision 425)
+++ /library/tests/test_util.h (revision 436)
@@ -15,6 +15,9 @@
 
 #include <string>
+#include "itpp_ext.h"
 
 namespace bdm {
+
+class epdf;
 
 /*! \brief Non-general but simple file load.
@@ -34,4 +37,10 @@
 bool remove_all(const char *path);
 
+double normcoef(const epdf *ep, const itpp::vec &xb, const itpp::vec &yb,
+		int xn = 100, int yn = 100);
+
+itpp::vec num_mean2(const epdf *ep, const itpp::vec &xb, const itpp::vec &yb,
+		    int xn = 100, int yn = 100);
+
 }
 
Index: /library/tests/egiw_test.cpp
===================================================================
--- /library/tests/egiw_test.cpp (revision 386)
+++ /library/tests/egiw_test.cpp (revision 436)
@@ -1,108 +1,94 @@
+#define BDMLIB // not an ideal way to prevent double registration of UI factories...
+#include "base/bdmbase.h"
+#include "base/user_info.h"
 #include "stat/exp_family.h"
+#include "itpp_ext.h"
+#include "epdf_harness.h"
+#include "mat_checks.h"
+#include "UnitTest++.h"
+
+const double epsilon = 0.00001;
+
+namespace UnitTest
+{
+
+inline void CheckClose(TestResults &results, const itpp::vec &expected,
+		       const itpp::vec &actual, double tolerance,
+		       TestDetails const &details) {
+    if (!AreClose(expected, actual, tolerance)) { 
+        MemoryOutStream stream;
+        stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;
+
+        results.OnTestFailure(details, stream.GetText());
+    }
+}
+
+}
+
 using namespace bdm;
 
-//These lines are needed for use of cout and endl
-using std::cout;
-using std::endl;
+template<>
+const ParticularUI<egiw> &ParticularUI<egiw>::factory(
+    ParticularUI<egiw>("egiw"));
 
-void Test ( const egiw &E ) {
+TEST(test_egiw) {
+    RV::clear_all();
+    UIFile in("egiw.cfg");
+    Array<epdf_harness *> input;
+    UI::get(input, in, "data");
+    int sz = input.size();
+    CHECK(sz > 0);
+    for (int i = 0; i < sz; ++i) {
+        input(i)->test();
+    }
 }
 
-int main() {
-	cout << "Testing eGiw(1,1)"<<endl;
-	{
-		// Setup model
-		double mu=1.1;
-		double s=0.1;
-
-		// TEST 1x1 EGIW
-		mat V ( 2,2 );
-		V ( 0,0 ) = pow ( mu,2 ) +s;
-		V ( 1,0 ) = mu;
-		V ( 0,1 ) = V ( 1,0 );
-		V ( 1,1 ) = 1.0;
-
-		double nu=10;
-
-		egiw E ( 1,nu*V,nu );
-		cout << "egiw mean value:" << E.mean() <<endl;
-		cout << "egiw normalizing constant:" << E.lognc() <<endl;
-
-		int n=100;
-		vec t_val ( 2 );
-
-		mat pdf ( 2*n,n );
-		vec Mu ( 2*n );
-		vec Si ( n );
-
-		for ( int i=0;i<2*n;i++ ) {
-			Mu ( i ) = -2+i* ( 1.0/ ( n ) ) *3.0;
-			t_val ( 0 ) = Mu ( i );
-			for ( int j=0;j<n;j++ ) {
-				Si ( j ) = ( j+1 ) * ( 1.0/n ) *2;
-				t_val ( 1 ) = Si ( j );
-
-				pdf ( i,j ) =E.evallog ( t_val );
-			}
-		}
-
-		mat Pdf=exp ( pdf );
-		vec fm=sum ( Pdf,2 );
-		vec fs=sum ( Pdf,1 );
-		cout << "Numerical mean: " << vec_2 ( Mu*fm/sum ( fm ), Si*fs/sum ( fs ) ) <<endl;
-		cout << "Numerical integral of pdf: "<<sumsum ( Pdf/n/n*3*2 ) <<endl;
-	}
-	cout << "Testing Egiw(1,2)"<<endl;
-	{
-		// Setup model
-		double mu=1.1; //unit step parametr
-		double b=3.0; // sequence of <1 -1 1 -1...>
-		double s=0.1;
+TEST(test_egiw_1_2) {
+    // Setup model
+    double mu = 1.1; //unit step parametr
+    double b = 3.0; // sequence of <1 -1 1 -1...>
+    double s = 0.1;
 
 
-		// TEST 1x1 EGIW
-		mat V ( 3,3 );
-		V ( 0,0 ) = pow ( mu,2 ) +pow ( b ,2 )  +s;
-		V ( 1,0 ) = mu;
-		V ( 2,0 ) = b;
+    // TEST 1x1 EGIW
+    mat V(3, 3);
+    V(0, 0) = pow(mu, 2) + pow(b, 2) + s;
+    V(1, 0) = mu;
+    V(2, 0) = b;
 
-		V ( 0,1 ) = V ( 1,0 );
-		V ( 1,1 ) = 1.0;
-		V ( 2,1 ) = 0.0;
+    V(0, 1) = V(1, 0);
+    V(1, 1) = 1.0;
+    V(2, 1) = 0.0;
 
-		V ( 0,2 ) = V ( 2,0 );
-		V ( 1,2 ) = V ( 2,1 );
-		V ( 2,2 ) = 1.0;
+    V(0, 2) = V(2, 0);
+    V(1, 2) = V(2, 1);
+    V(2, 2) = 1.0;
 
+    double nu = 20;
 
-		double nu=20;
+    egiw E(1, nu * V, nu);    
+    CHECK_CLOSE(vec("1.1 3.0 0.142857"), E.mean(), epsilon);
+    CHECK_CLOSE(7.36731, E.lognc(), epsilon);
 
-		egiw E ( 1,nu*V,nu );
-		cout << "egiw mean value:" << E.mean() <<endl;
-		cout << "egiw normalizing constant:" << E.lognc() <<endl;
+    int n = 100;
+    vec rgr(3);
 
-		int n=100;
-		vec t_val ( 3 );
+    mat Tmp(2 * n, n);
 
-		mat Tmp= zeros ( 2*n,n );
+    double summ = 0.0;
+    for (int k = 0; k < n; k++) { // ALL b
+	rgr(1) = 1 + k * (1.0 / n) * 4.0;
+	for (int i = 0; i < 2*n; i++) { //ALL mu
+	    rgr(0) = -2 + i * (1.0 / n) * 3.0;
+	    for (int j = 0; j < n; j++) { // All sigma
+	        rgr(2) = (j + 1) * (1.0 / n) * 2.0;
 
-		double summ=0.0;
-		for ( int k=0;k<n;k++ ) { // ALL b
-			t_val ( 1 ) = 1 + k* ( 1.0/n ) * 4.0;
-			for ( int i=0;i<2*n;i++ ) { //ALL mu
-				t_val ( 0 ) = -2+i* ( 1.0/  n ) *3.0;
-				for ( int j=0;j<n;j++ ) { // All sigma
-					t_val ( 2 ) = ( j+1 ) * ( 1.0/n ) *2.0;
+		Tmp(i, j) = E.evallog(rgr);
+	    }
+	}
+	summ += sumsum(exp(Tmp)) / n / n / n * 3.0 * 2.0 * 4.0;
+    }
 
-					Tmp ( i,j ) = E.evallog ( t_val );
-				}
-			}
-			summ += sumsum ( exp ( Tmp ) ) /n/n/n*3.0*2.0*4.0;
-		}
+    CHECK_CLOSE(1.0, summ, 0.1);
+}
 
-
-		cout << "Numerical integral of pdf: "<<summ <<endl;
-	}
-	
-	
-}
Index: /library/tests/CMakeLists.txt
===================================================================
--- /library/tests/CMakeLists.txt (revision 428)
+++ /library/tests/CMakeLists.txt (revision 436)
@@ -7,5 +7,5 @@
 link_directories (./unittest-cpp)
 
-add_library(testutil mat_checks.cpp mat_checks.h test_util.cpp test_util.h)
+add_library(testutil epdf_harness.cpp epdf_harness.h mat_checks.cpp mat_checks.h test_util.cpp test_util.h)
 target_link_libraries(testutil bdm itpp unittest)
 
@@ -17,5 +17,4 @@
 
 EXEC(fsqmat_test)
-EXEC(egiw_test)
 EXEC(emix_test)
 EXEC(test0)
@@ -39,5 +38,5 @@
 
 # using UnitTest++
-add_executable(testsuite datalink_test.cpp enorm_test.cpp loggers_test.cpp rv_test.cpp square_mat_test.cpp testsuite.cpp test_user_info.cpp test_shared_ptr.cpp)
+add_executable(testsuite datalink_test.cpp egiw_test.cpp enorm_test.cpp loggers_test.cpp rv_test.cpp square_mat_test.cpp testsuite.cpp test_user_info.cpp test_shared_ptr.cpp)
 target_link_libraries(testsuite bdm itpp testutil unittest)
 
Index: /library/tests/epdf_harness.cpp
===================================================================
--- /library/tests/epdf_harness.cpp (revision 436)
+++ /library/tests/epdf_harness.cpp (revision 436)
@@ -0,0 +1,72 @@
+#include "epdf_harness.h"
+#include "stat/exp_family.h"
+#include "mat_checks.h"
+#include "test_util.h"
+#include "UnitTest++.h"
+
+const double epsilon = 0.00001;
+
+namespace UnitTest
+{
+
+inline void CheckClose(TestResults &results, const itpp::vec &expected,
+		       const itpp::vec &actual, double tolerance,
+		       TestDetails const &details) {
+    if (!AreClose(expected, actual, tolerance)) { 
+        MemoryOutStream stream;
+        stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;
+
+        results.OnTestFailure(details, stream.GetText());
+    }
+}
+
+inline void CheckClose(TestResults &results, const itpp::mat &expected,
+		       const itpp::mat &actual, double tolerance,
+		       TestDetails const &details) {
+    if (!AreClose(expected, actual, tolerance)) { 
+        MemoryOutStream stream;
+        stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual;
+
+        results.OnTestFailure(details, stream.GetText());
+    }
+}
+
+}
+
+namespace bdm {
+
+template<>
+const ParticularUI<epdf_harness> &ParticularUI<epdf_harness>::factory(
+    ParticularUI<epdf_harness>("epdf_harness"));
+
+void epdf_harness::from_setting(const Setting &set) {
+    hepdf = UI::build<epdf>(set, "epdf");
+    UI::get(mean, set, "mean");
+    UI::get(variance, set, "variance");
+    UI::get(lognc, set, "lognc");
+    UI::get(support, set, "support");
+
+    if (set.exists("nsamples")) {
+        UI::get(nsamples, set, "nsamples");
+    }
+
+    UI::get(integral, set, "integral");
+}
+
+void epdf_harness::test()
+{
+    CHECK_CLOSE(mean, hepdf->mean(), epsilon);
+    CHECK_CLOSE(variance, hepdf->variance(), epsilon);
+
+    vec xb = support.get_row(0);
+    vec yb = support.get_row(1);
+    CHECK_CLOSE(mean, num_mean2(hepdf.get(), xb, yb, nsamples, nsamples), 0.1);
+    CHECK_CLOSE(integral, normcoef(hepdf.get(), xb, yb, nsamples, nsamples), 0.1);
+
+    egiw *wide = dynamic_cast<egiw *>(hepdf.get());
+    CHECK(wide);
+
+    CHECK_CLOSE(lognc, wide->lognc(), epsilon);
+}
+
+}
Index: /library/tests/test_util.cpp
===================================================================
--- /library/tests/test_util.cpp (revision 425)
+++ /library/tests/test_util.cpp (revision 436)
@@ -1,3 +1,5 @@
+#define BDMLIB // not an ideal way to prevent double registration of UI factories...
 #include "test_util.h"
+#include "stat/exp_family.h"
 #include <fstream>
 #include <stdexcept>
@@ -12,4 +14,7 @@
 
 namespace bdm {
+
+using itpp::vec;
+using itpp::mat;
 
 std::string load_test_file(const char *fname)
@@ -73,3 +78,52 @@
 }
 
+double normcoef(const epdf *ep, const vec &xb, const vec &yb,
+		int xn, int yn) {
+    mat Pdf(xn + 1, yn + 1);
+    vec rgr(2);
+
+    double xstep = (xb(1) - xb(0)) / xn;
+    double ystep = (yb(1) - yb(0)) / yn;
+
+    int i = 0;
+    for (double x = xb(0); x <= xb(1); x += xstep, i++) {
+        rgr(0) = x;
+	int j = 0;
+	for (double y = yb(0); y <= yb(1); y += ystep, j++) {
+	    rgr(1) = y;
+	    Pdf(i, j) = exp(ep->evallog(rgr));
+	}
+    }
+
+    return sumsum(Pdf) * xstep * ystep;
 }
+
+vec num_mean2(const epdf *ep, const vec &xb, const vec &yb,
+	      int xn, int yn) {
+    mat Pdf(xn + 1, yn + 1);
+    vec rgr(2);
+
+    double xstep = (xb(1) - xb(0)) / xn;
+    double ystep = (yb(1) - yb(0)) / yn;
+
+    vec Mu(xn + 1);
+    vec Si(yn + 1);
+
+    int i = 0;
+    for (double x = xb(0); x <= xb(1); x += xstep, i++) {
+        Mu(i) = x;
+	rgr(0) = x;
+	int j = 0;
+	for (double y = yb(0); y <= yb(1); y += ystep, j++) {
+	    Si(j) = y;
+	    rgr(1) = y;
+	    Pdf(i, j) = exp(ep->evallog(rgr));
+	}
+    }
+
+    vec fm = sum(Pdf, 2);
+    vec fs = sum(Pdf, 1);
+    return vec_2(Mu * fm / sum(fm), Si * fs / sum(fs));
+}
+
+}
Index: /library/tests/enorm_test.cpp
===================================================================
--- /library/tests/enorm_test.cpp (revision 428)
+++ /library/tests/enorm_test.cpp (revision 436)
@@ -3,4 +3,5 @@
 #include "stat/emix.h"
 #include "mat_checks.h"
+#include "test_util.h"
 #include "UnitTest++.h"
 
@@ -34,22 +35,4 @@
 }
 
-}
-
-double normcoef ( const epdf* ep,const vec &xb, const vec &yb, int Ngr=100 ) {
-	mat PPdf ( Ngr+1,Ngr+1 );
-	vec rgr ( 2 );
-
-	int i=0,j=0;
-	double xstep= ( xb ( 1 )-xb ( 0 ) ) /Ngr;
-	double ystep= ( yb ( 1 )-yb ( 0 ) ) /Ngr;
-
-	for ( double x=xb ( 0 );x<=xb ( 1 );x+= xstep,i++ ) {
-		rgr ( 0 ) =x;j=0;
-		for ( double y=yb ( 0 );y<=yb ( 1 );y+=ystep,j++ ) {
-			rgr ( 1 ) =y;
-			PPdf ( i,j ) =exp ( ep->evallog ( rgr ) );
-		}
-	}
-	return sumsum ( PPdf ) *xstep*ystep;
 }
 
