Connection::Column_metadata Connection::table_column_metadata(const std::string & table_name, const std::string & column_name, const std::string & db_name) { const char * type, * collation; int not_null, primary_key, auto_inc; int status = sqlite3_table_column_metadata(_db, db_name.c_str(), table_name.c_str(), column_name.c_str(), &type, &collation, ¬_null, &primary_key, &auto_inc); if(status != SQLITE_OK) { sqlite3_close(_db); throw Runtime_error("Error getting column info (" + db_name + "." + table_name + "." + column_name + "): " + sqlite3_errmsg(_db), "", status, nullptr); } Column_metadata ret; ret.type = type; ret.collation = collation; ret.not_null = bool(not_null); ret.primary_key = bool(primary_key); ret.auto_inc = bool(auto_inc); return ret; }
Connection::Connection(const std::string & filename) { int status = sqlite3_open(filename.c_str(), &_db); if(status != SQLITE_OK) { sqlite3_close(_db); throw Runtime_error("Error connecting to db (" + filename + "): " + sqlite3_errmsg(_db), "", status, nullptr); } sqlite3_extended_result_codes(_db, true); }
void test_perm(int N, int NN, unsigned int M) { int j; unsigned long i; double sum2 = 0; Array<long> count(0,NN-1); // count number of times each ball is drawn Array<int> last(0,NN-1); // details of last draw long consec = 0; // count number of consecutive balls last = 0; count = 0; // set the arrays to zero cout << endl; cout << "Size of urn = " << NN << "; "; cout << "no. of balls = " << N << "; "; cout << "no. of trials = " << M << endl; cout << BaseTest::Header << endl; if (N > NN) { Throw(Runtime_error("N > NN")); } long double S0 = 0.0; // sum of ball numbers long double S1 = 0.0; // sum of ball * draw double centre = (NN+1) / 2.0; // average ball number double avj = (N+1)/2.0; // average draw number Array2<int> table1(0,NN-1,0,N-1); // times ball i occurs at draw j Array2<int> table2(0,NN-1,0,NN-1); // times ball i and ball j both occur table1 = 0; table2 = 0; // clear the tables long turnings = 0; // number of turning points RandomPermutation RP; // reset the Urn Array<int> Draw(0,N-1); // for the draw for (i=1; i<=M; i++) // iterate over total number of trials { int last_ball = -1; RP.Next(NN,N,Draw.base(),1); // do the draw Array<int> check(0,NN-1); check = 0; // for checking no repeats for (j = 1; j <= N; j++) // N balls in draw { int x = Draw(j-1); if (x<1 || x>NN) Throw(Runtime_error("Invalid number")); check(x-1)++; count(x-1)++; sum2 += last(x-1); if (x == last_ball+1 || x == last_ball-1) consec++; last_ball = x; double xc = x - centre; S0 += xc; S1 += xc * (j-avj); table1(x-1,j-1)++; } for (j=0; j<NN; j++) { if (check(j) > 1) Throw(Runtime_error("Repeated number")); last(j) = check(j); if (check(j)) // increment table of pairs { for (int k = j+1; k<NN; k++) if (check(k)) table2(j,k)++; } } // count number of turnings for (j = 2; j < N; j++) { long last = Draw(j-2); long current = Draw(j-1); long next = Draw(j); if ( (last < current && next < current) || (last > current && next > current) ) turnings++; } } // do the tests double sum1 = 0.0; double d = M * (N / (double)NN); for (j=0; j<NN; j++) { sum1 += square(count(j) - d); } if (N < NN) { sum1 /= d; double sd = (NN - N) * sqrt( 2 * (M - 1) / (M * ((double)(NN - 1))) ); NormalTestTwoSided freq_test("Ball freq.", sum1, NN - N, square(sd)); freq_test.DoTest(); NormalTestTwoSided repeats_test("Repeats", sum2, (M - 1) * (N * N / (double)NN), square((double)(N * (NN - N)) / (double)NN) * (M - 1) / (NN - 1) ); repeats_test.DoTest(); double V0 = ((double)N * (NN+1.0) * (NN-N))/12.0; NormalTestTwoSided average_test("Average", S0, 0, M * V0); average_test.DoTest(); } if (N > 1) { double V1 = (double)N * (N*N - 1) * NN * (NN + 1) / 144.0; NormalTestTwoSided ball_times_draw("Ball * draw", S1, 0, M * V1); ball_times_draw.DoTest(); double e = 2 * M * (N - 1) / (double)NN; // expected # of consecutive balls NormalTestTwoSided consec_test("Consec balls", consec, e, e); consec_test.DoTest(); } if (N > 1) { // balls vs draws double CS = 0; double ev = (double)M / NN; for (int i = 0; i < NN; i++) for (int j = 0; j < N; j++) CS += square(table1(i,j)-ev); CS /= ev; ev = N * (NN - 1); double v = 2.0 * N * (M - 1) * (N - (2 - NN) * NN) / M / (NN - 1); NormalTestTwoSided ball_draw("Ball vs draw", CS, ev, v); ball_draw.DoTest(); } if (N>1 && N < NN) { // pairs of balls double CS = 0; double ev = (double)M*N*(N-1)/ NN / (NN-1); for (int i = 0; i < NN; i++) for (int j = i+1; j < NN; j++) CS += square(table2(i,j)-ev); CS /= ev; ev = (NN-N)*(NN+N-1)/2.0; double v = (double)(M-1)*square(NN-N)*(-3+6*N-3.0*N*N+2*NN-6.0*N*NN+ 2.0*N*N*NN+NN*NN)/(M*(NN-2)*(NN-3)); NormalTestTwoSided pairs_test("Pairs table", CS, ev, v); pairs_test.DoTest(); } if (N > 2) { double V2 = (16.0 * N - 29.0) / 90.0; NormalTestTwoSided runs_test("Runs test", turnings, (double)M * (N-2) / 1.5, M * V2); runs_test.DoTest(); } }