int main(int argc, char *argv[]) { int p[2]; int stdoutfd; struct obj exp; (void)argc; (void)argv; printf("1..1\n"); fflush(stdout); stderrfd = dup(STDERR_FILENO); if (stderrfd < 0) err(1, "dup of stderr failed"); stdoutfd = dup(STDOUT_FILENO); if (stdoutfd < 0) err(1, "dup of stdout failed"); if (pipe(p) != 0) failmsg("pipe failed"); if (dup2(p[1], STDERR_FILENO) < 0 || dup2(p[1], STDOUT_FILENO) < 0) failmsg("Duplicating file descriptor"); plan_tests(10); expect(p[0], "1..10\n"); ok(1, "msg1"); expect(p[0], "ok 1 - msg1\n"); ok(0, "msg2"); expect(p[0], "not ok 2 - msg2\n" "# Failed test (*tap/test/run.c:main() at line 194)\n"); ok1(true); expect(p[0], "ok 3 - true\n"); ok1(false); expect(p[0], "not ok 4 - false\n" "# Failed test (*tap/test/run.c:main() at line 201)\n"); pass("passed"); expect(p[0], "ok 5 - passed\n"); fail("failed"); expect(p[0], "not ok 6 - failed\n" "# Failed test (*tap/test/run.c:main() at line 208)\n"); skip(2, "skipping %s", "test"); expect(p[0], "ok 7 # skip skipping test\n" "ok 8 # skip skipping test\n"); todo_start("todo"); ok1(false); expect(p[0], "not ok 9 - false # TODO todo\n" "# Failed (TODO) test (*tap/test/run.c:main() at line 217)\n"); ok1(true); expect(p[0], "ok 10 - true # TODO todo\n"); todo_end(); if (exit_status() != 3) failmsg("Expected exit status 3, not %i", exit_status()); is(one_int(), 1, "one_int() returns 1"); expect(p[0], "ok 11 - one_int() returns 1\n"); is(one_int(), 2, "one_int() returns 2"); expect(p[0], "not ok 12 - one_int() returns 2\n" "# Failed test (*tap/test/run.c:main() at line 229)\n" "# got: 1\n" "# expected: 2\n"); is_eq(one_str(), "one", "one_str() returns 'one'"); expect(p[0], "ok 13 - one_str() returns 'one'\n"); is_eq(one_str(), "two", "one_str() returns 'two'"); expect(p[0], "not ok 14 - one_str() returns 'two'\n" "# Failed test (*tap/test/run.c:main() at line 237)\n" "# got: \"one\"\n" "# expected: \"two\"\n"); exp.id = 1; is_cmp(one_obj(), &exp, obj_cmp, obj_to_str, "one_obj() has id 1"); expect(p[0], "ok 15 - one_obj() has id 1\n"); exp.id = 2; is_cmp(one_obj(), &exp, obj_cmp, obj_to_str, "one_obj() has id 2"); expect(p[0], "not ok 16 - one_obj() has id 2\n" "# Failed test (*tap/test/run.c:main() at line 247)\n" "# got: {id=1}\n" "# expected: {id=2}\n"); #if 0 /* Manually run the atexit command. */ _cleanup(); expect(p[0], "# Looks like you failed 2 tests of 9.\n"); #endif write_all(stdoutfd, "ok 1 - All passed\n", strlen("ok 1 - All passed\n")); _exit(0); }
int main(int argc, char *argv[]) { unsigned int i, j; int num; struct trav_data td; TDB_DATA k; struct tdb_context *tdb; union tdb_attribute seed_attr; enum TDB_ERROR ecode; int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP, TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT }; seed_attr.base.attr = TDB_ATTRIBUTE_SEED; seed_attr.base.next = &tap_log_attr; seed_attr.seed.seed = 6334326220117065685ULL; plan_tests(sizeof(flags) / sizeof(flags[0]) * (NUM_RECORDS*6 + (NUM_RECORDS-1)*3 + 22) + 1); for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) { tdb = tdb_open("run-traverse.tdb", flags[i], O_RDWR|O_CREAT|O_TRUNC, 0600, &seed_attr); ok1(tdb); if (!tdb) continue; ok1(tdb_firstkey(tdb, &k) == TDB_ERR_NOEXIST); /* One entry... */ k.dptr = (unsigned char *)# k.dsize = sizeof(num); num = 0; ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0); ok1(tdb_firstkey(tdb, &k) == TDB_SUCCESS); ok1(k.dsize == sizeof(num)); ok1(memcmp(k.dptr, &num, sizeof(num)) == 0); ok1(tdb_nextkey(tdb, &k) == TDB_ERR_NOEXIST); /* Two entries. */ k.dptr = (unsigned char *)# k.dsize = sizeof(num); num = 1; ok1(tdb_store(tdb, k, k, TDB_INSERT) == 0); ok1(tdb_firstkey(tdb, &k) == TDB_SUCCESS); ok1(k.dsize == sizeof(num)); memcpy(&num, k.dptr, sizeof(num)); ok1(num == 0 || num == 1); ok1(tdb_nextkey(tdb, &k) == TDB_SUCCESS); ok1(k.dsize == sizeof(j)); memcpy(&j, k.dptr, sizeof(j)); ok1(j == 0 || j == 1); ok1(j != num); ok1(tdb_nextkey(tdb, &k) == TDB_ERR_NOEXIST); /* Clean up. */ k.dptr = (unsigned char *)# k.dsize = sizeof(num); num = 0; ok1(tdb_delete(tdb, k) == 0); num = 1; ok1(tdb_delete(tdb, k) == 0); /* Now lots of records. */ ok1(store_records(tdb)); td.calls = 0; num = tdb_traverse(tdb, trav, &td); ok1(num == NUM_RECORDS); ok1(td.calls == NUM_RECORDS); /* Simple loop should match tdb_traverse */ for (j = 0, ecode = tdb_firstkey(tdb, &k); j < td.calls; j++) { int val; ok1(ecode == TDB_SUCCESS); ok1(k.dsize == sizeof(val)); memcpy(&val, k.dptr, k.dsize); ok1(td.records[j] == val); ecode = tdb_nextkey(tdb, &k); } /* But arbitrary orderings should work too. */ for (j = td.calls-1; j > 0; j--) { k.dptr = (unsigned char *)&td.records[j-1]; k.dsize = sizeof(td.records[j-1]); k = dup_key(k); ok1(tdb_nextkey(tdb, &k) == TDB_SUCCESS); ok1(k.dsize == sizeof(td.records[j])); ok1(memcmp(k.dptr, &td.records[j], k.dsize) == 0); free(k.dptr); } /* Even delete should work. */ for (j = 0, ecode = tdb_firstkey(tdb, &k); ecode != TDB_ERR_NOEXIST; j++) { ok1(ecode == TDB_SUCCESS); ok1(k.dsize == 4); ok1(tdb_delete(tdb, k) == 0); ecode = tdb_nextkey(tdb, &k); } diag("delete using first/nextkey gave %u of %u records", j, NUM_RECORDS); ok1(j == NUM_RECORDS); tdb_close(tdb); } ok1(tap_log_messages == 0); return exit_status(); }
static void finish_ok(struct io_conn *conn, struct data *d) { ok1(d->state == 2); d->state++; io_break(d, io_idle()); }
int main(int argc, char *argv[]) { int fds[2]; struct child_data daemonized; pid_t pid; plan_tests(5); if (pipe(fds) != 0) err(1, "Failed pipe"); /* Since daemonize forks and parent exits, we need to fork * that parent. */ pid = fork(); if (pid == -1) err(1, "Failed fork"); if (pid == 0) { char buffer[2]; pid = getpid(); daemonize(0, 0, 1); daemon_is_ready(); /* Keep valgrind happy about uninitialized bytes. */ memset(&daemonized, 0, sizeof(daemonized)); daemonized.pid = getpid(); daemonized.in_root_dir = (getcwd(buffer, 2) != NULL); daemonized.read_from_stdin = read(STDIN_FILENO, buffer, 1) == -1 ? errno : 0; daemonized.write_to_stdout = write(STDOUT_FILENO, buffer, 1) == -1 ? errno : 0; if (write(STDERR_FILENO, buffer, 1) != 1) { daemonized.write_to_stderr = errno; if (daemonized.write_to_stderr == 0) daemonized.write_to_stderr = -1; } else daemonized.write_to_stderr = 0; /* Make sure parent exits. */ while (getppid() == pid) sleep(1); daemonized.ppid = getppid(); if (write(fds[1], &daemonized, sizeof(daemonized)) != sizeof(daemonized)) exit(1); exit(0); } if (read(fds[0], &daemonized, sizeof(daemonized)) != sizeof(daemonized)) err(1, "Failed read"); ok1(daemonized.pid != pid); #if 0 /* Believe it or not, this fails under Ubuntu 13.10 (Upstart) */ ok1(daemonized.ppid == 1); #endif ok1(daemonized.in_root_dir); ok1(daemonized.read_from_stdin == 0); ok1(daemonized.write_to_stdout == 0); ok1(daemonized.write_to_stderr == 0); return exit_status(); }
static void TestOpenAir() { Airspaces airspaces; if (!ParseFile(Path(_T("test/data/airspace/openair.txt")), airspaces)) { skip(3, 0, "Failed to parse input file"); return; } const AirspaceClassTestCouple classes[] = { { _T("Class-R-Test"), RESTRICT }, { _T("Class-Q-Test"), DANGER }, { _T("Class-P-Test"), PROHIBITED }, { _T("Class-CTR-Test"), CTR }, { _T("Class-A-Test"), CLASSA }, { _T("Class-B-Test"), CLASSB }, { _T("Class-C-Test"), CLASSC }, { _T("Class-D-Test"), CLASSD }, { _T("Class-GP-Test"), NOGLIDER }, { _T("Class-W-Test"), WAVE }, { _T("Class-E-Test"), CLASSE }, { _T("Class-F-Test"), CLASSF }, { _T("Class-TMZ-Test"), TMZ }, { _T("Class-G-Test"), CLASSG }, { _T("Class-RMZ-Test"), RMZ }, }; ok1(airspaces.GetSize() == 24); const auto range = airspaces.QueryAll(); for (auto it = range.begin(); it != range.end(); ++it) { const AbstractAirspace &airspace = it->GetAirspace(); if (StringIsEqual(_T("Circle-Test"), airspace.GetName())) { if (!ok1(airspace.GetShape() == AbstractAirspace::Shape::CIRCLE)) continue; const AirspaceCircle &circle = (const AirspaceCircle &)airspace; ok1(equals(circle.GetRadius(), Units::ToSysUnit(5, Unit::NAUTICAL_MILES))); ok1(equals(circle.GetReferenceLocation(), Angle::Degrees(1.091667), Angle::Degrees(0.091667))); } else if (StringIsEqual(_T("Polygon-Test"), airspace.GetName())) { if (!ok1(airspace.GetShape() == AbstractAirspace::Shape::POLYGON)) continue; const AirspacePolygon &polygon = (const AirspacePolygon &)airspace; const SearchPointVector &points = polygon.GetPoints(); if (!ok1(points.size() == 5)) continue; ok1(equals(points[0].GetLocation(), Angle::DMS(1, 30, 30), Angle::DMS(1, 30, 30, true))); ok1(equals(points[1].GetLocation(), Angle::DMS(1, 30, 30), Angle::DMS(1, 30, 30))); ok1(equals(points[2].GetLocation(), Angle::DMS(1, 30, 30, true), Angle::DMS(1, 30, 30))); ok1(equals(points[3].GetLocation(), Angle::DMS(1, 30, 30, true), Angle::DMS(1, 30, 30, true))); ok1(equals(points[4].GetLocation(), Angle::DMS(1, 30, 30), Angle::DMS(1, 30, 30, true))); } else if (StringIsEqual(_T("Radio-Test"), airspace.GetName())) { ok1(StringIsEqual(_T("130.125 MHz"), airspace.GetRadioText().c_str())); } else if (StringIsEqual(_T("Height-Test-1"), airspace.GetName())) { ok1(airspace.GetBase().IsTerrain()); ok1(airspace.GetTop().reference == AltitudeReference::MSL); ok1(equals(airspace.GetTop().altitude, Units::ToSysUnit(2000, Unit::FEET))); } else if (StringIsEqual(_T("Height-Test-2"), airspace.GetName())) { ok1(airspace.GetBase().reference == AltitudeReference::MSL); ok1(equals(airspace.GetBase().altitude, 0)); ok1(airspace.GetTop().reference == AltitudeReference::STD); ok1(equals(airspace.GetTop().flight_level, 65)); } else if (StringIsEqual(_T("Height-Test-3"), airspace.GetName())) { ok1(airspace.GetBase().reference == AltitudeReference::AGL); ok1(equals(airspace.GetBase().altitude_above_terrain, Units::ToSysUnit(100, Unit::FEET))); ok1(airspace.GetTop().reference == AltitudeReference::MSL); ok1(airspace.GetTop().altitude > Units::ToSysUnit(30000, Unit::FEET)); } else if (StringIsEqual(_T("Height-Test-4"), airspace.GetName())) { ok1(airspace.GetBase().reference == AltitudeReference::MSL); ok1(equals(airspace.GetBase().altitude, 100)); ok1(airspace.GetTop().reference == AltitudeReference::MSL); ok1(airspace.GetTop().altitude > Units::ToSysUnit(30000, Unit::FEET)); } else if (StringIsEqual(_T("Height-Test-5"), airspace.GetName())) { ok1(airspace.GetBase().reference == AltitudeReference::AGL); ok1(equals(airspace.GetBase().altitude, 100)); ok1(airspace.GetTop().reference == AltitudeReference::MSL); ok1(equals(airspace.GetTop().altitude, 450)); } else if (StringIsEqual(_T("Height-Test-6"), airspace.GetName())) { ok1(airspace.GetBase().reference == AltitudeReference::AGL); ok1(equals(airspace.GetBase().altitude_above_terrain, Units::ToSysUnit(50, Unit::FEET))); ok1(airspace.GetTop().reference == AltitudeReference::STD); ok1(equals(airspace.GetTop().flight_level, 50)); } else { for (unsigned i = 0; i < ARRAY_SIZE(classes); ++i) { if (StringIsEqual(classes[i].name, airspace.GetName())) ok1(airspace.GetType() == classes[i].type); } } } }
int main(int argc, char **argv) { plan_tests(92); // test constructor GeoPoint p1(Angle::Degrees(345.32), Angle::Degrees(-6.332)); ok1(p1.IsValid()); ok1(equals(p1, -6.332, 345.32)); // test normalize() p1.Normalize(); ok1(p1.IsValid()); ok1(equals(p1, -6.332, -14.68)); // test parametric() GeoPoint p2(Angle::Degrees(2), Angle::Degrees(1)); GeoPoint p3 = p1.Parametric(p2, 5); ok1(p2.IsValid()); ok1(p3.IsValid()); ok1(equals(p3, -1.332, -4.68)); // test interpolate GeoPoint p4 = p1.Interpolate(p3, 0.5); ok1(p4.IsValid()); ok1(equals(p4, -3.832, -9.68)); GeoPoint p5 = p1.Interpolate(p3, 0.25); ok1(p5.IsValid()); ok1(equals(p5, -5.082, -12.18)); // test * GeoPoint p6 = p2 * 3.5; ok1(p6.IsValid()); ok1(equals(p6, 3.5, 7)); // test + p6 = p6 + p2; ok1(p6.IsValid()); ok1(equals(p6, 4.5, 9)); // test += p6 += p2; ok1(p6.IsValid()); ok1(equals(p6, 5.5, 11)); // test - p6 = p6 - p2; ok1(p6.IsValid()); ok1(equals(p6, 4.5, 9)); // for large and short distance testing GeoPoint p11(Angle::Degrees(0.00001), Angle::Degrees(0.00001)); GeoPoint p12(Angle::Degrees(179), Angle::Degrees(0)); p11 += p1; p12 += p1; ok1(p11.IsValid()); ok1(equals(p11, -6.33199, -14.67999)); ok1(p12.IsValid()); ok1(equals(p12, -6.332, 164.32)); // test sort() ok1(!p1.Sort(p3)); ok1(p3.Sort(p1)); ok1(!p1.Sort(p4)); ok1(p4.Sort(p1)); ok1(!p1.Sort(p5)); ok1(p5.Sort(p1)); ok1(!p4.Sort(p3)); ok1(p3.Sort(p4)); ok1(!p5.Sort(p3)); ok1(p3.Sort(p5)); ok1(!p5.Sort(p4)); ok1(p4.Sort(p5)); // test distance() // // note: distance between p1 and p4 and between p3 and p4 is not // the same due to linear interpolation instead of real geographic // intermediate point calculation ok1(equals(p2.Distance(p6), 869146.334126)); ok1(equals(p6.Distance(p2), 869146.334126)); ok1(equals(p1.Distance(p5), 309506.275043)); ok1(equals(p1.Distance(p4), 619486.719361)); ok1(equals(p1.Distance(p3), 1240403.22926)); ok1(equals(p3.Distance(p4), 620924.169000)); ok1(equals(p1.Distance(p11), 1.561761)); ok1(equals(p1.Distance(p12), 18599361.600)); ok1(equals(p2.DistanceS(p6), 869326.653160)); ok1(equals(p6.DistanceS(p2), 869326.653160)); ok1(equals(p1.DistanceS(p5), 309562.219016)); ok1(equals(p1.DistanceS(p4), 619603.149273)); ok1(equals(p1.DistanceS(p3), 1240649.267606)); ok1(equals(p3.DistanceS(p4), 621053.760625)); ok1(equals(p1.DistanceS(p11), 1.568588)); ok1(equals(p1.DistanceS(p12), 18602548.701)); // test bearing() // // note: the bearings p1 -> p5, p5 -> p4 and so on are not the same due to // linear interpolation instead of real geographic intermediate point // calculation ok1(equals(p2.Bearing(p6), 63.425773)); ok1(equals(p6.Bearing(p2), 243.762198)); ok1(equals(p1.Bearing(p5), 63.601900)); ok1(equals(p1.Bearing(p4), 63.735395)); ok1(equals(p1.Bearing(p3), 63.937616)); ok1(equals(p5.Bearing(p4), 63.619712)); ok1(equals(p5.Bearing(p3), 63.799336)); ok1(equals(p4.Bearing(p3), 63.694155)); ok1(equals(p5.Bearing(p6), 66.126880)); ok1(equals(p2.Bearing(p3), 250.886912)); ok1(equals(p2.BearingS(p6), 63.272424)); ok1(equals(p6.BearingS(p2), 243.608847)); ok1(equals(p1.BearingS(p5), 63.449343)); ok1(equals(p1.BearingS(p4), 63.582620)); ok1(equals(p1.BearingS(p3), 63.784526)); ok1(equals(p5.BearingS(p4), 63.466726)); ok1(equals(p5.BearingS(p3), 63.646072)); ok1(equals(p4.BearingS(p3), 63.540756)); ok1(equals(p5.BearingS(p6), 65.982854)); ok1(equals(p2.BearingS(p3), 250.786774)); // test distance_bearing() // note: should be the same output as bearing() and distance() GeoVector v = p2.DistanceBearing(p6); ok1(equals(v.distance, 869146.334126)); ok1(equals(v.bearing, 63.425773)); v = p2.DistanceBearingS(p6); ok1(equals(v.distance, 869326.653160)); ok1(equals(v.bearing, 63.272424)); // test intermediate_point() GeoPoint p7(Angle::Zero(), Angle::Zero()); ok1(p7.IsValid()); GeoPoint p8 = p7.IntermediatePoint(p2, 100000); ok1(p8.IsValid()); ok1(equals(p8, 0.402361, 0.804516)); ok1(equals(p8.Distance(p7), 100000)); GeoPoint p9 = p7.IntermediatePoint(p2, 100000000); ok1(p9.IsValid()); ok1(equals(p9, p2)); // test projected_distance() ok1(equals(p8.ProjectedDistance(p7, p2), 100000)); ok1(equals(p4.ProjectedDistance(p1, p3), 619494.517917)); ok1(equals((p2 * 2).ProjectedDistance(p2, p6), 248511.833322)); // Tests moved here from test_fixed.cpp GeoPoint l1(Angle::Zero(), Angle::Zero()); ok1(l1.IsValid()); GeoPoint l2(Angle::Degrees(-0.3), Angle::Degrees(1.0)); ok1(l2.IsValid()); GeoPoint l3(Angle::Degrees(0.00001), Angle::Zero()); ok1(l3.IsValid()); GeoPoint l4(Angle::Degrees(10), Angle::Zero()); ok1(l4.IsValid()); l4.SetInvalid(); ok1(!l4.IsValid()); bool find_lat_lon_okay = true; for (Angle bearing = Angle::Zero(); bearing < Angle::FullCircle(); bearing += Angle::Degrees(5)) { GeoPoint p_test = FindLatitudeLongitude(p1, bearing, 50000); find_lat_lon_okay = equals(p_test.Distance(p1), 50000) && find_lat_lon_okay; } ok1(find_lat_lon_okay); v = l1.DistanceBearing(l2); // 116090 @ 343 v = l1.DistanceBearing(l3); ok(v.distance > 0 && v.distance < 2, "earth distance short", 0); GeoPoint p10(GeoPoint::Invalid()); ok1(!p10.IsValid()); return exit_status(); }
int main(int argc, char *argv[]) { unsigned int i; uintptr_t perfect_bit; struct htable ht; uint64_t val[NUM_VALS]; uint64_t dne; void *p; struct htable_iter iter; plan_tests(29); for (i = 0; i < NUM_VALS; i++) val[i] = i; dne = i; htable_init(&ht, hash, NULL); ok1(ht.max == 0); ok1(ht.bits == 0); /* We cannot find an entry which doesn't exist. */ ok1(!htable_get(&ht, hash(&dne, NULL), objcmp, &dne)); /* This should increase it once. */ add_vals(&ht, val, 0, 1); ok1(ht.bits == 1); ok1(ht.max == 1); ok1(ht.common_mask == -1); /* Mask should be set. */ ok1(check_mask(&ht, val, 1)); /* This should increase it again. */ add_vals(&ht, val, 1, 1); ok1(ht.bits == 2); ok1(ht.max == 3); /* Mask should be set. */ ok1(ht.common_mask != 0); ok1(ht.common_mask != -1); ok1(check_mask(&ht, val, 2)); /* Now do the rest. */ add_vals(&ht, val, 2, NUM_VALS - 2); /* Find all. */ find_vals(&ht, val, NUM_VALS); ok1(!htable_get(&ht, hash(&dne, NULL), objcmp, &dne)); /* Walk once, should get them all. */ i = 0; for (p = htable_first(&ht,&iter); p; p = htable_next(&ht, &iter)) i++; ok1(i == NUM_VALS); /* Delete all. */ del_vals(&ht, val, NUM_VALS); ok1(!htable_get(&ht, hash(&val[0], NULL), objcmp, &val[0])); /* Worst case, a "pointer" which doesn't have any matching bits. */ htable_add(&ht, 0, (void *)~(uintptr_t)&val[NUM_VALS-1]); htable_add(&ht, hash(&val[NUM_VALS-1], NULL), &val[NUM_VALS-1]); ok1(ht.common_mask == 0); ok1(ht.common_bits == 0); /* Get rid of bogus pointer before we trip over it! */ htable_del(&ht, 0, (void *)~(uintptr_t)&val[NUM_VALS-1]); /* Add the rest. */ add_vals(&ht, val, 0, NUM_VALS-1); /* Check we can find them all. */ find_vals(&ht, val, NUM_VALS); ok1(!htable_get(&ht, hash(&dne, NULL), objcmp, &dne)); /* Corner cases: wipe out the perfect bit using bogus pointer. */ htable_clear(&ht); htable_add(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1])); ok1(ht.perfect_bit); perfect_bit = ht.perfect_bit; htable_add(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1] | perfect_bit)); ok1(ht.perfect_bit == 0); htable_del(&ht, 0, (void *)((uintptr_t)&val[NUM_VALS-1] | perfect_bit)); /* Enlarging should restore it... */ add_vals(&ht, val, 0, NUM_VALS-1); ok1(ht.perfect_bit != 0); htable_clear(&ht); return exit_status(); }
int main(int argc, char *argv[]) { struct tdb_context *tdb; TDB_DATA key, data; plan_tests(13); tdb = tdb_open_ex("run-check.tdb", 1, TDB_CLEAR_IF_FIRST, O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL); ok1(tdb); ok1(tdb_check(tdb, NULL, NULL) == 0); key.dsize = strlen("hi"); key.dptr = discard_const_p(uint8_t, "hi"); data.dsize = strlen("world"); data.dptr = discard_const_p(uint8_t, "world"); ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); tdb = tdb_open_ex("run-check.tdb", 1024, 0, O_RDWR, 0, &taplogctx, NULL); ok1(tdb); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); tdb = tdb_open_ex("test/tdb.corrupt", 1024, 0, O_RDWR, 0, &taplogctx, NULL); ok1(tdb); ok1(tdb_check(tdb, NULL, NULL) == -1); ok1(tdb_error(tdb) == TDB_ERR_CORRUPT); tdb_close(tdb); /* Big and little endian should work! */ tdb = tdb_open_ex("test/old-nohash-le.tdb", 1024, 0, O_RDWR, 0, &taplogctx, NULL); ok1(tdb); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); tdb = tdb_open_ex("test/old-nohash-be.tdb", 1024, 0, O_RDWR, 0, &taplogctx, NULL); ok1(tdb); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); return exit_status(); }
int main(int argc, char *argv[]) { struct tdb_context *tdb; TDB_DATA key, data; plan_tests(13); tdb = tdb_open_ex("run-endian.tdb", 1024, TDB_CLEAR_IF_FIRST|TDB_CONVERT, O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL); ok1(tdb); key.dsize = strlen("hi"); key.dptr = (void *)"hi"; data.dsize = strlen("world"); data.dptr = (void *)"world"; ok1(tdb_store(tdb, key, data, TDB_MODIFY) < 0); ok1(tdb_error(tdb) == TDB_ERR_NOEXIST); ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0); ok1(tdb_store(tdb, key, data, TDB_INSERT) < 0); ok1(tdb_error(tdb) == TDB_ERR_EXISTS); ok1(tdb_store(tdb, key, data, TDB_MODIFY) == 0); data = tdb_fetch(tdb, key); ok1(data.dsize == strlen("world")); ok1(memcmp(data.dptr, "world", strlen("world")) == 0); free(data.dptr); key.dsize++; data = tdb_fetch(tdb, key); ok1(data.dptr == NULL); tdb_close(tdb); /* Reopen: should read it */ tdb = tdb_open_ex("run-endian.tdb", 1024, 0, O_RDWR, 0, &taplogctx, NULL); ok1(tdb); key.dsize = strlen("hi"); key.dptr = (void *)"hi"; data = tdb_fetch(tdb, key); ok1(data.dsize == strlen("world")); ok1(memcmp(data.dptr, "world", strlen("world")) == 0); free(data.dptr); tdb_close(tdb); return exit_status(); }
static void TestExpiration() { ClimbAverageCalculator c; c.Reset(); constexpr double AVERAGE_TIME = 30; // Test expiration for empty data ok1(c.Expired(0, 60)); ok1(c.Expired(15, 60)); // Add values and test non-expiration bool expired = false; for (unsigned i = 1; i <= 60; i++) { c.GetAverage(i, i, AVERAGE_TIME); expired = expired || c.Expired(i, 60); } ok1(!expired); // Test expiration with 30sec ok1(!c.Expired(89, 30)); ok1(!c.Expired(90, 30)); ok1(c.Expired(91, 30)); // Test expiration with 60sec ok1(!c.Expired(119, 60)); ok1(!c.Expired(120, 60)); ok1(c.Expired(121, 60)); // Time warp ok1(c.Expired(59, 60)); ok1(!c.Expired(60, 60)); ok1(!c.Expired(61, 60)); }
int main(void) { plan_tests(37); ok1(rszshm_mk(NULL, 0, NULL) == NULL && errno == EINVAL); struct rszshm s, t; ok1(rszshm_mk(&s, 0, NULL) == NULL && errno == EINVAL); ok1(rszshm_mk(&s, 4096, longstr) == NULL && errno == EINVAL); fail_mmap_anon = 1; ok1(rszshm_mk(&s, 4096, NULL) == NULL && errno == 9010); rszshm_rm(&s); fail_mmap_anon = 0; fail_open = 1; ok1(rszshm_mk(&s, 4096, NULL) == NULL && errno == 9005); rszshm_rm(&s); fail_open = 0; fail_ftruncate = 1; ok1(rszshm_mk(&s, 4096, NULL) == NULL && errno == 9002); rszshm_rm(&s); fail_ftruncate = 0; fail_mmap_fixed = 1; ok1(rszshm_mk(&s, 4096, NULL) == NULL && errno == 9011); rszshm_rm(&s); fail_mmap_fixed = 0; fail_msync = 1; ok1(rszshm_mk(&s, 4096, NULL) == NULL && errno == 9003); rszshm_rm(&s); fail_msync = 0; ok1(rszshm_mk(&s, 4096, NULL) != NULL); struct rszshm_scan scan = RSZSHM_DFLT_SCAN; scan.iter = 1; ok1(rszshm_mk(&t, 4096, NULL, scan) == NULL && errno == ENOSPC); ok1(rszshm_dt(&s) == 0); ok1(rszshm_rm(&s) == 0); long pgsz = sysconf(_SC_PAGE_SIZE); scan.len = UINT64_MAX - pgsz; ok1(rszshm_mk(&t, 4096, NULL, scan) == NULL && errno == ENOMEM); ok1(rszshm_mk(&t, 4096, "foo/bar_XXXXXX/0") == NULL && errno == ENOENT); struct rszshm *r; ok1(rszshm_mkm(r, 4096, NULL) != NULL); pid_t p, *pp; noerr(p = fork()); char *fname = strdupa(r->fname); if (p) waitpid(p, NULL, 0); else { ok1(rszshm_free(r) == 0); struct rszshm *q; ok1(rszshm_atm(q, fname) != NULL); *((pid_t *) q->dat) = getpid(); ok1(rszshm_up(q) == 0); ok1(rszshm_grow(q) == 1); ok1(rszshm_free(q) == 0); exit(0); } pp = (pid_t *) r->dat; ok1(p == *pp); fail_mmap_fixed = 1; ok1(rszshm_up(r) == -1 && errno == 9011); fail_mmap_fixed = 0; ok1(rszshm_grow(r) == 1); ok1(rszshm_dt(r) == 0); sa.sa_handler = segvjmp; sa.sa_flags = SA_RESETHAND; sigemptyset(&sa.sa_mask); sigaction(SIGSEGV, &sa, NULL); if (setjmp(j) == 0) fail("still mapped after detach: %d", *pp); else pass("access after detach gives segv, OK!"); ok1(rszshm_at(r, longstr) == NULL && errno == EINVAL); fail_open = 1; ok1(rszshm_at(r, fname) == NULL && errno == 9005); fail_open = 0; fail_read = 1; ok1(rszshm_at(r, fname) == NULL && errno == 9006); fail_read = 0; short_read = 1; ok1(rszshm_at(r, fname) == NULL && errno == ENODATA); short_read = 0; fail_mmap_anon = 1; ok1(rszshm_at(r, fname) == NULL && errno == 9010); fail_mmap_anon = 0; bad_mmap_addr = 1; ok1(rszshm_at(r, fname) == NULL && errno == ENOSPC); bad_mmap_addr = 0; fail_mmap_fixed = 1; ok1(rszshm_at(r, fname) == NULL && errno == 9011); fail_mmap_fixed = 0; ok1(rszshm_at(r, fname) != NULL); ok1(p == *pp); struct rszshm_hdr save = *r->hdr; r->hdr->flen = r->flen; r->hdr->max = r->flen; ok1(rszshm_grow(r) == -1 && errno == ENOMEM); *r->hdr = save; fail_flock = 1; ok1(rszshm_grow(r) == -1 && errno == 9001); fail_flock = 0; fail_ftruncate = 1; ok1(rszshm_grow(r) == -1 && errno == 9002); fail_ftruncate = 0; ok1(rszshm_grow(r) == 1); ok1(rszshm_dt(r) == 0); ok1(rszshm_rm(r) == 0); r->fname[0] = '\0'; ok1(rszshm_rmdir(r) == -1 && errno == ENOTDIR); ok1(rszshm_free(r) == 0); return exit_status(); }
int main(int argc, char *argv[]) { struct tdb_context *tdb; struct tdb_header hdr; int fd; plan_tests(11); /* Can open fine if complete crap, as long as O_CREAT. */ fd = open("run-bad-tdb-header.tdb", O_RDWR|O_CREAT|O_TRUNC, 0600); ok1(fd >= 0); ok1(write(fd, "hello world", 11) == 11); close(fd); tdb = tdb_open_ex("run-bad-tdb-header.tdb", 1024, 0, O_RDWR, 0, &taplogctx, NULL); ok1(!tdb); tdb = tdb_open_ex("run-bad-tdb-header.tdb", 1024, 0, O_CREAT|O_RDWR, 0600, &taplogctx, NULL); ok1(tdb); tdb_close(tdb); /* Now, with wrong version it should *not* overwrite. */ fd = open("run-bad-tdb-header.tdb", O_RDWR); ok1(fd >= 0); ok1(read(fd, &hdr, sizeof(hdr)) == sizeof(hdr)); ok1(hdr.version == TDB_VERSION); hdr.version++; lseek(fd, 0, SEEK_SET); ok1(write(fd, &hdr, sizeof(hdr)) == sizeof(hdr)); close(fd); tdb = tdb_open_ex("run-bad-tdb-header.tdb", 1024, 0, O_RDWR|O_CREAT, 0600, &taplogctx, NULL); ok1(errno == EIO); ok1(!tdb); /* With truncate, will be fine. */ tdb = tdb_open_ex("run-bad-tdb-header.tdb", 1024, 0, O_RDWR|O_CREAT|O_TRUNC, 0600, &taplogctx, NULL); ok1(tdb); tdb_close(tdb); return exit_status(); }
int main(int argc, char *argv[]) { char *bad; plan_tests(14); /* Correct guesses. */ one_seems_likely(1); ok1(likely_stats(0, 90) == NULL); one_seems_unlikely(2); ok1(likely_stats(0, 90) == NULL); /* Incorrect guesses. */ one_seems_likely(0); one_seems_likely(2); /* Hasn't been hit 4 times, so this fails */ ok1(!likely_stats(4, 90)); bad = likely_stats(3, 90); ok(strends(bad, "run-debug.c:9:likely(val == 1) correct 33% (1/3)"), "likely_stats returned %s", bad); free(bad); /* Nothing else above 90% */ ok1(!likely_stats(0, 90)); /* This should get everything. */ bad = likely_stats(0, 100); ok(strends(bad, "run-debug.c:16:unlikely(val == 1) correct 100% (1/1)"), "likely_stats returned %s", bad); free(bad); /* Nothing left (table is actually cleared) */ ok1(!likely_stats(0, 100)); /* Make sure unlikely works */ one_seems_unlikely(0); one_seems_unlikely(2); one_seems_unlikely(1); bad = likely_stats(0, 90); ok(strends(bad, "run-debug.c:16:unlikely(val == 1) correct 66% (2/3)"), "likely_stats returned %s", bad); free(bad); ok1(!likely_stats(0, 100)); likely_one_unlikely_two(1, 1); likely_one_unlikely_two(1, 1); likely_one_unlikely_two(1, 1); ok1(!likely_stats(0, 90)); likely_one_unlikely_two(1, 2); bad = likely_stats(0, 90); ok(strends(bad, "run-debug.c:24:unlikely(val2 == 2) correct 75% (3/4)"), "likely_stats returned %s", bad); free(bad); bad = likely_stats(0, 100); ok(strends(bad, "run-debug.c:24:likely(val1 == 1) correct 100% (4/4)"), "likely_stats returned %s", bad); free(bad); ok1(!likely_stats(0, 100)); /* Check that reset works! */ one_seems_unlikely(0); one_seems_unlikely(2); one_seems_unlikely(1); likely_stats_reset(); ok1(!likely_stats(0, 100)); exit(exit_status()); }
int main(int argc, char *argv[]) { struct tdb_context *tdb; unsigned int log_count; TDB_DATA d; union tdb_attribute log_attr, jhash_attr, ohash_attr, incompat_hash_attr; log_attr.base.attr = TDB_ATTRIBUTE_LOG; log_attr.base.next = NULL; log_attr.log.fn = log_fn; log_attr.log.data = &log_count; jhash_attr.base.attr = TDB_ATTRIBUTE_HASH; jhash_attr.base.next = &log_attr; jhash_attr.hash.fn = jenkins_hashfn; ohash_attr.base.attr = TDB_ATTRIBUTE_HASH; ohash_attr.base.next = &log_attr; ohash_attr.hash.fn = old_hash; incompat_hash_attr.base.attr = TDB_ATTRIBUTE_HASH; incompat_hash_attr.base.next = &log_attr; incompat_hash_attr.hash.fn = tdb1_incompatible_hash; plan_tests(28); /* Create with default hash. */ log_count = 0; tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1, O_CREAT|O_RDWR|O_TRUNC, 0600, &log_attr); ok1(tdb); ok1(log_count == 0); d.dptr = (void *)"Hello"; d.dsize = 5; ok1(tdb_store(tdb, d, d, TDB_INSERT) == TDB_SUCCESS); tdb_close(tdb); /* Fail to open with different hash. */ tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1, O_RDWR, 0, &jhash_attr); ok1(!tdb); ok1(log_count == 1); /* Create with different hash. */ log_count = 0; tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1, O_CREAT|O_RDWR|O_TRUNC, 0600, &jhash_attr); ok1(tdb); ok1(log_count == 0); tdb_close(tdb); /* Endian should be no problem. */ log_count = 0; tdb = tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1, O_RDWR, 0, &ohash_attr); ok1(!tdb); ok1(log_count == 1); log_count = 0; tdb = tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1, O_RDWR, 0, &ohash_attr); ok1(!tdb); ok1(log_count == 1); log_count = 0; /* Fail to open with old default hash. */ tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1, O_RDWR, 0, &ohash_attr); ok1(!tdb); ok1(log_count == 1); log_count = 0; tdb = tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1, O_RDONLY, 0, &incompat_hash_attr); ok1(tdb); ok1(log_count == 0); ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS); tdb_close(tdb); log_count = 0; tdb = tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1, O_RDONLY, 0, &incompat_hash_attr); ok1(tdb); ok1(log_count == 0); ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS); tdb_close(tdb); /* It should open with jenkins hash if we don't specify. */ log_count = 0; tdb = tdb_open("test/jenkins-le-hash.tdb1", TDB_VERSION1, O_RDWR, 0, &log_attr); ok1(tdb); ok1(log_count == 0); ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS); tdb_close(tdb); log_count = 0; tdb = tdb_open("test/jenkins-be-hash.tdb1", TDB_VERSION1, O_RDWR, 0, &log_attr); ok1(tdb); ok1(log_count == 0); ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS); tdb_close(tdb); log_count = 0; tdb = tdb_open("run-wronghash-fail.tdb1", TDB_VERSION1, O_RDONLY, 0, &log_attr); ok1(tdb); ok1(log_count == 0); ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS); tdb_close(tdb); return exit_status(); }
int main(int argc, char *argv[]) { tdb_off_t b_off, test; struct tdb_context *tdb; struct tdb_layout *layout; struct tdb_data data, key; tdb_len_t len; /* FIXME: Test TDB_CONVERT */ /* FIXME: Test lock order fail. */ plan_tests(42); data = tdb_mkdata("world", 5); key = tdb_mkdata("hello", 5); /* No coalescing can be done due to EOF */ layout = new_tdb_layout("run-03-coalesce.tdb"); tdb_layout_add_freetable(layout); len = 1024; tdb_layout_add_free(layout, len, 0); tdb = tdb_layout_get(layout); ok1(tdb_check(tdb, NULL, NULL) == 0); ok1(free_record_length(tdb, layout->elem[1].base.off) == len); /* Figure out which bucket free entry is. */ b_off = bucket_off(tdb->ftable_off, size_to_bucket(len)); /* Lock and fail to coalesce. */ ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0); test = layout->elem[1].base.off; ok1(coalesce(tdb, layout->elem[1].base.off, b_off, len, &test) == 0); tdb_unlock_free_bucket(tdb, b_off); ok1(free_record_length(tdb, layout->elem[1].base.off) == len); ok1(test == layout->elem[1].base.off); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); tdb_layout_free(layout); /* No coalescing can be done due to used record */ layout = new_tdb_layout("run-03-coalesce.tdb"); tdb_layout_add_freetable(layout); tdb_layout_add_free(layout, 1024, 0); tdb_layout_add_used(layout, key, data, 6); tdb = tdb_layout_get(layout); ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024); ok1(tdb_check(tdb, NULL, NULL) == 0); /* Figure out which bucket free entry is. */ b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024)); /* Lock and fail to coalesce. */ ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0); test = layout->elem[1].base.off; ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024, &test) == 0); tdb_unlock_free_bucket(tdb, b_off); ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024); ok1(test == layout->elem[1].base.off); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); tdb_layout_free(layout); /* Coalescing can be done due to two free records, then EOF */ layout = new_tdb_layout("run-03-coalesce.tdb"); tdb_layout_add_freetable(layout); tdb_layout_add_free(layout, 1024, 0); tdb_layout_add_free(layout, 2048, 0); tdb = tdb_layout_get(layout); ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024); ok1(free_record_length(tdb, layout->elem[2].base.off) == 2048); ok1(tdb_check(tdb, NULL, NULL) == 0); /* Figure out which bucket (first) free entry is. */ b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024)); /* Lock and coalesce. */ ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0); test = layout->elem[2].base.off; ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024, &test) == 1024 + sizeof(struct tdb_used_record) + 2048); /* Should tell us it's erased this one... */ ok1(test == TDB_ERR_NOEXIST); ok1(tdb->file->allrecord_lock.count == 0 && tdb->file->num_lockrecs == 0); ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024 + sizeof(struct tdb_used_record) + 2048); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); tdb_layout_free(layout); /* Coalescing can be done due to two free records, then data */ layout = new_tdb_layout("run-03-coalesce.tdb"); tdb_layout_add_freetable(layout); tdb_layout_add_free(layout, 1024, 0); tdb_layout_add_free(layout, 512, 0); tdb_layout_add_used(layout, key, data, 6); tdb = tdb_layout_get(layout); ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024); ok1(free_record_length(tdb, layout->elem[2].base.off) == 512); ok1(tdb_check(tdb, NULL, NULL) == 0); /* Figure out which bucket free entry is. */ b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024)); /* Lock and coalesce. */ ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0); test = layout->elem[2].base.off; ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024, &test) == 1024 + sizeof(struct tdb_used_record) + 512); ok1(tdb->file->allrecord_lock.count == 0 && tdb->file->num_lockrecs == 0); ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024 + sizeof(struct tdb_used_record) + 512); ok1(test == TDB_ERR_NOEXIST); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); tdb_layout_free(layout); /* Coalescing can be done due to three free records, then EOF */ layout = new_tdb_layout("run-03-coalesce.tdb"); tdb_layout_add_freetable(layout); tdb_layout_add_free(layout, 1024, 0); tdb_layout_add_free(layout, 512, 0); tdb_layout_add_free(layout, 256, 0); tdb = tdb_layout_get(layout); ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024); ok1(free_record_length(tdb, layout->elem[2].base.off) == 512); ok1(free_record_length(tdb, layout->elem[3].base.off) == 256); ok1(tdb_check(tdb, NULL, NULL) == 0); /* Figure out which bucket free entry is. */ b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024)); /* Lock and coalesce. */ ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0); test = layout->elem[2].base.off; ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024, &test) == 1024 + sizeof(struct tdb_used_record) + 512 + sizeof(struct tdb_used_record) + 256); ok1(tdb->file->allrecord_lock.count == 0 && tdb->file->num_lockrecs == 0); ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024 + sizeof(struct tdb_used_record) + 512 + sizeof(struct tdb_used_record) + 256); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); tdb_layout_free(layout); ok1(tap_log_messages == 0); return exit_status(); }
int main(void) { struct cdump_definitions *defs; const struct cdump_type *t, *p; char *ctx = tal(NULL, char), *problems; /* This is how many tests you plan to run */ plan_tests(111); defs = cdump_extract(ctx, "enum foo CDUMP(foo note) { BAR CDUMP(bar note) };", NULL); ok1(defs); ok1(tal_parent(defs) == ctx); ok1(strmap_empty(&defs->structs)); ok1(strmap_empty(&defs->unions)); t = strmap_get(&defs->enums, "foo"); ok1(t); ok1(t->kind == CDUMP_ENUM); ok1(streq(t->note, "foo note")); ok1(streq(t->name, "foo")); ok1(tal_count(t->u.enum_vals) == 1); ok1(streq(t->u.enum_vals[0].name, "BAR")); ok1(!t->u.enum_vals[0].value); ok1(streq(t->u.enum_vals[0].note, "bar note")); defs = cdump_extract(ctx, "enum foo { BAR CDUMP(bar note) = 7 };", &problems); ok1(defs); ok1(tal_parent(defs) == ctx); ok1(!problems); ok1(strmap_empty(&defs->structs)); ok1(strmap_empty(&defs->unions)); t = strmap_get(&defs->enums, "foo"); ok1(t); ok1(t->kind == CDUMP_ENUM); ok1(streq(t->name, "foo")); ok1(tal_count(t->u.enum_vals) == 1); ok1(streq(t->u.enum_vals[0].name, "BAR")); ok1(streq(t->u.enum_vals[0].value, "7")); ok1(streq(t->u.enum_vals[0].note, "bar note")); defs = cdump_extract(ctx, "enum foo {\n" "BAR CDUMP(bar note) = 7,\n" "BAZ CDUMP(baz note),\n" "FUZZ CDUMP(fuzz note) };", &problems); ok1(defs); ok1(tal_parent(defs) == ctx); ok1(!problems); ok1(strmap_empty(&defs->structs)); ok1(strmap_empty(&defs->unions)); t = strmap_get(&defs->enums, "foo"); ok1(t); ok1(t->kind == CDUMP_ENUM); ok1(streq(t->name, "foo")); ok1(t->note == NULL); ok1(tal_count(t->u.enum_vals) == 3); ok1(streq(t->u.enum_vals[0].name, "BAR")); ok1(streq(t->u.enum_vals[0].value, "7")); ok1(streq(t->u.enum_vals[0].note, "bar note")); ok1(streq(t->u.enum_vals[1].name, "BAZ")); ok1(streq(t->u.enum_vals[1].note, "baz note")); ok1(!t->u.enum_vals[1].value); ok1(streq(t->u.enum_vals[2].name, "FUZZ")); ok1(streq(t->u.enum_vals[2].note, "fuzz note")); ok1(!t->u.enum_vals[2].value); defs = cdump_extract(ctx, "struct foo CDUMP(foo note) { int x CDUMP(x note); };", &problems); ok1(defs); ok1(tal_parent(defs) == ctx); ok1(!problems); ok1(strmap_empty(&defs->enums)); ok1(strmap_empty(&defs->unions)); t = strmap_get(&defs->structs, "foo"); ok1(t); ok1(t->kind == CDUMP_STRUCT); ok1(streq(t->name, "foo")); ok1(streq(t->note, "foo note")); ok1(tal_count(t->u.members) == 1); ok1(streq(t->u.members[0].name, "x")); ok1(streq(t->u.members[0].note, "x note")); ok1(t->u.members[0].type->kind == CDUMP_UNKNOWN); ok1(streq(t->u.members[0].type->name, "int")); defs = cdump_extract(ctx, "struct foo { int x[5<< 1] CDUMP(x note); struct foo *next CDUMP(next note); struct unknown **ptrs[10] CDUMP(ptrs note); };", &problems); ok1(defs); ok1(tal_parent(defs) == ctx); ok1(!problems); ok1(strmap_empty(&defs->enums)); ok1(strmap_empty(&defs->unions)); t = strmap_get(&defs->structs, "foo"); ok1(t); ok1(t->kind == CDUMP_STRUCT); ok1(streq(t->name, "foo")); ok1(tal_count(t->u.members) == 3); ok1(streq(t->u.members[0].name, "x")); ok1(streq(t->u.members[0].note, "x note")); ok1(t->u.members[0].type->kind == CDUMP_ARRAY); ok1(streq(t->u.members[0].type->u.arr.size, "5<< 1")); ok1(t->u.members[0].type->u.arr.type->kind == CDUMP_UNKNOWN); ok1(streq(t->u.members[0].type->u.arr.type->name, "int")); ok1(streq(t->u.members[1].name, "next")); ok1(streq(t->u.members[1].note, "next note")); ok1(t->u.members[1].type->kind == CDUMP_POINTER); ok1(t->u.members[1].type->u.ptr == t); ok1(streq(t->u.members[2].name, "ptrs")); ok1(streq(t->u.members[2].note, "ptrs note")); p = t->u.members[2].type; ok1(p->kind == CDUMP_ARRAY); ok1(streq(p->u.arr.size, "10")); p = p->u.arr.type; ok1(p->kind == CDUMP_POINTER); p = p->u.ptr; ok1(p->kind == CDUMP_POINTER); p = p->u.ptr; ok1(p->kind == CDUMP_STRUCT); ok1(streq(p->name, "unknown")); ok1(p->u.members == NULL); /* We don't put undefined structs into definition maps. */ ok1(!strmap_get(&defs->structs, "unknown")); /* unions and comments. */ defs = cdump_extract(ctx, "#if 0\n" "/* Normal comment */\n" "struct foo { int x[5 * 7/* Comment */]CDUMP(x note/*nocomment*/); };\n" "// One-line comment\n" "union bar CDUMP(bar note) { enum sometype x CDUMP(x note// Comment\n" "); union yun// Comment\n" "y;};\n" "#endif", &problems); ok1(defs); ok1(tal_parent(defs) == ctx); ok1(!problems); t = strmap_get(&defs->structs, "foo"); ok1(t); ok1(t->note == NULL); ok1(tal_count(t->u.members) == 1); ok1(streq(t->u.members[0].name, "x")); ok1(streq(t->u.members[0].note, "x note")); ok1(t->u.members[0].type->kind == CDUMP_ARRAY); ok1(streq(t->u.members[0].type->u.arr.size, "5 * 7")); ok1(t->u.members[0].type->u.arr.type->kind == CDUMP_UNKNOWN); ok1(streq(t->u.members[0].type->u.arr.type->name, "int")); t = strmap_get(&defs->unions, "bar"); ok1(t); ok1(streq(t->note, "bar note")); ok1(tal_count(t->u.members) == 2); ok1(streq(t->u.members[0].name, "x")); ok1(streq(t->u.members[0].note, "x note")); ok1(t->u.members[0].type->kind == CDUMP_ENUM); ok1(streq(t->u.members[0].type->name, "sometype")); ok1(!t->u.members[0].type->u.enum_vals); ok1(streq(t->u.members[1].name, "y")); ok1(t->u.members[1].note == NULL); ok1(t->u.members[1].type->kind == CDUMP_UNION); ok1(streq(t->u.members[1].type->name, "yun")); ok1(!t->u.members[1].type->u.members); /* This exits depending on whether all tests passed */ return exit_status(); }
int main(void) { const int iter = get_iter(0, 1); const bool oth_first = CHECK_FLAG(iter, 1); diag("oth_first=%s", btos(oth_first)); plan_tests(1); sem_t *sems[] = { &goahead_sem, &done_sem }; for(int i=0; i < NUM_ELEMENTS(sems); i++) { int n = sem_init(sems[i], 0, 0); if(n != 0) { perror("sem_init"); abort(); } } int *data = malloc(sizeof(*data) * 2); data[0] = 100; data[1] = 100; pthread_t other; int n = pthread_create(&other, NULL, &other_fn, data); if(n != 0) { perror("pthread_create"); abort(); } int status; bool posted = false; do { status = xn_begin(); diag("main txn started"); int v0 = xn_read_int(&data[0]), v1 = xn_read_int(&data[1]); if(oth_first && !posted) { sem_post(&goahead_sem); sem_wait(&done_sem); posted = true; } v1 -= 200; if(v0 + v1 < 0) { diag("invariant broken in main"); break; } xn_put(&data[1], v1); } while(status = xn_commit(), XN_RESTART(status)); xn_abort(status); diag("main txn done"); if(!posted) { assert(!oth_first); sem_post(&goahead_sem); sem_wait(&done_sem); } void *retval = NULL; n = pthread_join(other, &retval); if(n != 0) { perror("pthread_join"); abort(); } if(!ok1(data[0] + data[1] >= 0)) { diag("data[0]=%d, data[1]=%d", data[0], data[1]); } free(data); for(int i=0; i < NUM_ELEMENTS(sems); i++) sem_destroy(sems[i]); return exit_status(); }
int main(void) { unsigned char array[ARR_SIZE]; unsigned int i, prev; plan_tests(405); prev = 0; /* Test encode_length */ for (i = 1; i < 0x8000000; i *= 2) { ok1(encode_length(i-1) >= prev); ok1(encode_length(i) >= encode_length(i-1)); ok1(encode_length(i+1) >= encode_length(i)); prev = encode_length(i); } /* Test it against actual encoding return val. */ for (i = 1; i < 0x8000000; i *= 2) { ok1(encode_length(i-1) == encode(i - 1 + MIN_BLOCK_SIZE, false, array)); ok1(encode_length(i) == encode(i + MIN_BLOCK_SIZE, false, array)); ok1(encode_length(i+1) == encode(i + 1 + MIN_BLOCK_SIZE, false, array)); } /* Test encoder vs. decoder. */ for (i = 1; i < 0x8000000; i *= 2) { unsigned long hdrlen, len; bool free; hdrlen = encode(i - 1 + MIN_BLOCK_SIZE, false, array); ok1(decode(&len, &free, array) == hdrlen); ok1(len == i - 1 + MIN_BLOCK_SIZE); ok1(free == false); hdrlen = encode(i + MIN_BLOCK_SIZE, true, array); ok1(decode(&len, &free, array) == hdrlen); ok1(len == i + MIN_BLOCK_SIZE); ok1(free == true); hdrlen = encode(i + 1 + MIN_BLOCK_SIZE, true, array); ok1(decode(&len, &free, array) == hdrlen); ok1(len == i + 1 + MIN_BLOCK_SIZE); ok1(free == true); } return exit_status(); }
int main(void) { char buf[64]; char *sret; int iret; plan_tests(27); /* * strerror() failure tests. */ errno = 0; sret = strerror(INT_MAX); snprintf(buf, sizeof(buf), "Unknown error: %d", INT_MAX); ok1(strcmp(sret, buf) == 0); ok1(errno == EINVAL); /* * strerror() success tests. */ errno = 0; sret = strerror(0); ok1(strcmp(sret, "No error: 0") == 0); ok1(errno == 0); errno = 0; sret = strerror(EPERM); ok1(strcmp(sret, "Operation not permitted") == 0); ok1(errno == 0); errno = 0; sret = strerror(EPFNOSUPPORT); ok1(strcmp(sret, "Protocol family not supported") == 0); ok1(errno == 0); errno = 0; sret = strerror(ELAST); ok1(errno == 0); /* * strerror_r() failure tests. */ memset(buf, '*', sizeof(buf)); iret = strerror_r(-1, buf, sizeof(buf)); ok1(strcmp(buf, "Unknown error: -1") == 0); ok1(iret == EINVAL); memset(buf, '*', sizeof(buf)); /* One byte too short. */ iret = strerror_r(EPERM, buf, strlen("Operation not permitted")); ok1(strcmp(buf, "Operation not permitte") == 0); ok1(iret == ERANGE); memset(buf, '*', sizeof(buf)); /* One byte too short. */ iret = strerror_r(-1, buf, strlen("Unknown error: -1")); ok1(strcmp(buf, "Unknown error: -") == 0); ok1(iret == EINVAL); memset(buf, '*', sizeof(buf)); /* Two bytes too short. */ iret = strerror_r(-2, buf, strlen("Unknown error: -2") - 1); ok1(strcmp(buf, "Unknown error: ") == 0); ok1(iret == EINVAL); memset(buf, '*', sizeof(buf)); /* Three bytes too short. */ iret = strerror_r(-2, buf, strlen("Unknown error: -2") - 2); ok1(strcmp(buf, "Unknown error:") == 0); ok1(iret == EINVAL); memset(buf, '*', sizeof(buf)); /* One byte too short. */ iret = strerror_r(12345, buf, strlen("Unknown error: 12345")); ok1(strcmp(buf, "Unknown error: 1234") == 0); ok1(iret == EINVAL); /* * strerror_r() success tests. */ memset(buf, '*', sizeof(buf)); iret = strerror_r(0, buf, sizeof(buf)); ok1(strcmp(buf, "No error: 0") == 0); ok1(iret == 0); memset(buf, '*', sizeof(buf)); iret = strerror_r(EDEADLK, buf, sizeof(buf)); ok1(strcmp(buf, "Resource deadlock avoided") == 0); ok1(iret == 0); memset(buf, '*', sizeof(buf)); iret = strerror_r(EPROCLIM, buf, sizeof(buf)); ok1(strcmp(buf, "Too many processes") == 0); ok1(iret == 0); return exit_status(); }
void GlidePolarTest::TestBasic() { polar.Update(); ok1(equals(polar.polar.a, polar.ideal_polar.a)); ok1(equals(polar.polar.b, polar.ideal_polar.b)); ok1(equals(polar.polar.c, polar.ideal_polar.c)); ok1(equals(polar.SinkRate(Units::ToSysUnit(fixed(80), Unit::KILOMETER_PER_HOUR)), 0.606)); ok1(equals(polar.SinkRate(Units::ToSysUnit(fixed(120), Unit::KILOMETER_PER_HOUR)), 0.99)); ok1(equals(polar.SinkRate(Units::ToSysUnit(fixed(160), Unit::KILOMETER_PER_HOUR)), 1.918)); ok1(equals(polar.GetSMax(), polar.SinkRate(polar.GetVMax()))); ok1(equals(polar.GetVMin(), 19.934640523)); ok1(equals(polar.GetSMin(), polar.SinkRate(polar.GetVMin()))); ok1(equals(polar.GetVTakeoff(), polar.GetVMin() / 2)); ok1(equals(polar.GetVBestLD(), 25.830434162)); ok1(equals(polar.GetSBestLD(), polar.SinkRate(polar.GetVBestLD()))); ok1(equals(polar.GetBestLD(), polar.GetVBestLD() / polar.GetSBestLD())); ok1(equals(polar.GetTotalMass(), 318)); ok1(equals(polar.GetWingLoading(), 32.448979592)); ok1(equals(polar.GetBallast(), 0)); ok1(equals(polar.GetBallastLitres(), 0)); ok1(polar.IsBallastable()); ok1(!polar.HasBallast()); }
int main(int argc, char **argv) { plan_tests(2 * ARRAY_SIZE(valid) + 2 * ARRAY_SIZE(invalid) + 2 * ARRAY_SIZE(length) + 4 * ARRAY_SIZE(crop) + ARRAY_SIZE(latin1_chars) + #ifndef _UNICODE ARRAY_SIZE(truncate_string_tests) + #endif 9 + 27); for (auto i : valid) { ok1(ValidateUTF8(i)); ok1(LengthUTF8(i) == MyLengthUTF8(i)); } for (auto i : invalid) { ok1(!ValidateUTF8(i)); ok1(!MyValidateUTF8(i)); } for (auto &l : length) { ok1(l.length == LengthUTF8(l.value)); ok1(l.length == MyLengthUTF8(l.value)); } char buffer[64]; for (auto &l : latin1_chars) { *Latin1ToUTF8(l.ch, buffer) = 0; ok1(strcmp(l.utf8, buffer) == 0); } for (auto &c : crop) { strcpy(buffer, c.input); auto *end = CropIncompleteUTF8(buffer); ok1(strcmp(c.output, buffer) == 0); ok1(end != nullptr); ok1(*end == '\0'); ok1(end == buffer + strlen(buffer)); } #ifndef _UNICODE TestTruncateString(); #endif { const char *p = "foo\xe7\x9b\xae"; auto n = NextUTF8(p); ok1(n.first == 'f'); ok1(n.second == p + 1); n = NextUTF8(p + 1); ok1(n.first == 'o'); ok1(n.second == p + 2); n = NextUTF8(p + 2); ok1(n.first == 'o'); ok1(n.second == p + 3); n = NextUTF8(p + 3); ok1(n.first == 30446); ok1(n.second == p + 6); n = NextUTF8(p + 6); ok1(n.first == 0); } /* test UnicodeToUTF8() */ buffer[0] = 1; ok1(UnicodeToUTF8(0, buffer) == buffer + 1); ok1(buffer[0] == 0); ok1(UnicodeToUTF8(' ', buffer) == buffer + 1); ok1(buffer[0] == ' '); ok1(UnicodeToUTF8(0x7f, buffer) == buffer + 1); ok1(buffer[0] == 0x7f); ok1(UnicodeToUTF8(0xa2, buffer) == buffer + 2); ok1(buffer[0] == char(0xc2)); ok1(buffer[1] == char(0xa2)); ok1(UnicodeToUTF8(0x6fb3, buffer) == buffer + 3); ok1(buffer[0] == char(0xe6)); ok1(buffer[1] == char(0xbe)); ok1(buffer[2] == char(0xb3)); ok1(UnicodeToUTF8(0xffff, buffer) == buffer + 3); ok1(buffer[0] == char(0xef)); ok1(buffer[1] == char(0xbf)); ok1(buffer[2] == char(0xbf)); ok1(UnicodeToUTF8(0x10000, buffer) == buffer + 4); ok1(buffer[0] == char(0xf0)); ok1(buffer[1] == char(0x90)); ok1(buffer[2] == char(0x80)); ok1(buffer[3] == char(0x80)); ok1(UnicodeToUTF8(0x10ffff, buffer) == buffer + 4); ok1(buffer[0] == char(0xf4)); ok1(buffer[1] == char(0x8f)); ok1(buffer[2] == char(0xbf)); ok1(buffer[3] == char(0xbf)); return exit_status(); }
void GlidePolarTest::TestBallast() { polar.SetBallast(fixed(0.25)); ok1(equals(polar.GetBallastLitres(), 25)); ok1(equals(polar.GetBallast(), 0.25)); polar.SetBallastLitres(fixed(50)); ok1(equals(polar.GetBallastLitres(), 50)); ok1(equals(polar.GetBallast(), 0.5)); ok1(equals(polar.GetTotalMass(), 368)); ok1(equals(polar.GetWingLoading(), 37.551020408)); ok1(polar.HasBallast()); fixed loading_factor = sqrt(polar.GetTotalMass() / polar.reference_mass); ok1(equals(polar.polar.a, polar.ideal_polar.a / loading_factor)); ok1(equals(polar.polar.b, polar.ideal_polar.b)); ok1(equals(polar.polar.c, polar.ideal_polar.c * loading_factor)); ok1(equals(polar.SinkRate(Units::ToSysUnit(fixed(80), Unit::KILOMETER_PER_HOUR)), 0.640739)); ok1(equals(polar.SinkRate(Units::ToSysUnit(fixed(120), Unit::KILOMETER_PER_HOUR)), 0.928976)); ok1(equals(polar.SinkRate(Units::ToSysUnit(fixed(160), Unit::KILOMETER_PER_HOUR)), 1.722908)); ok1(equals(polar.GetVMin(), 21.44464)); ok1(equals(polar.GetVBestLD(), 27.78703)); polar.SetBallast(fixed(0)); ok1(!polar.HasBallast()); }
int main(void) { char *parent, *c; plan_tests(21); /* We can take NULL. */ ok1(take(NULL) == NULL); ok1(is_taken(NULL)); ok1(taken(NULL)); /* Undoes take() */ ok1(!is_taken(NULL)); ok1(!taken(NULL)); parent = tal(NULL, char); ok1(parent); ok1(take(parent) == parent); ok1(is_taken(parent)); ok1(taken(parent)); /* Undoes take() */ ok1(!is_taken(parent)); ok1(!taken(parent)); c = tal(parent, char); *c = 'h'; c = tal_dup(parent, char, take(c), 1, 0); ok1(c[0] == 'h'); ok1(tal_parent(c) == parent); c = tal_dup(parent, char, take(c), 1, 2); ok1(c[0] == 'h'); strcpy(c, "hi"); ok1(tal_parent(c) == parent); /* dup must reparent child. */ c = tal_dup(NULL, char, take(c), 1, 0); ok1(c[0] == 'h'); ok1(tal_parent(c) == NULL); /* No leftover allocations. */ tal_free(c); ok1(talloc_total_blocks(parent) == 1); tal_free(parent); ok1(!taken_any()); /* NULL pass-through. */ c = NULL; ok1(tal_dup(NULL, char, take(c), 5, 5) == NULL); ok1(!taken_any()); return exit_status(); }
int main(int argc, char *argv[]) { struct tdb_context *tdb; TDB_DATA key, orig_data, data; uint32_t hashval; tdb_off_t rec_ptr; struct tdb_record rec; int ret; plan_tests(24); tdb = tdb_open_ex("run-36-file.tdb", 1024, TDB_CLEAR_IF_FIRST, O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL); ok1(tdb); tdb->methods = &large_io_methods; key.dsize = strlen("hi"); key.dptr = (void *)"hi"; orig_data.dsize = strlen("world"); orig_data.dptr = (void *)"world"; /* Enlarge the file (internally multiplies by 2). */ ret = tdb_expand(tdb, 1500000000); #ifdef HAVE_INCOHERENT_MMAP /* This can fail due to mmap failure on 32 bit systems. */ if (ret == -1) { /* These should now fail. */ ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == -1); data = tdb_fetch(tdb, key); ok1(data.dptr == NULL); ok1(tdb_traverse(tdb, test_traverse, &orig_data) == -1); ok1(tdb_delete(tdb, key) == -1); ok1(tdb_traverse(tdb, test_traverse, NULL) == -1); /* Skip the rest... */ for (ret = 0; ret < 24 - 6; ret++) ok1(1); tdb_close(tdb); return exit_status(); } #endif ok1(ret == 0); /* Put an entry in, and check it. */ ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == 0); data = tdb_fetch(tdb, key); ok1(data.dsize == strlen("world")); ok1(memcmp(data.dptr, "world", strlen("world")) == 0); free(data.dptr); /* That currently fills at the end, make sure that's true. */ hashval = tdb->hash_fn(&key); rec_ptr = tdb_find_lock_hash(tdb, key, hashval, F_RDLCK, &rec); ok1(rec_ptr); ok1(rec_ptr > 2U*1024*1024*1024); tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK); /* Traverse must work. */ ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1); /* Delete should work. */ ok1(tdb_delete(tdb, key) == 0); ok1(tdb_traverse(tdb, test_traverse, NULL) == 0); /* Transactions should work. */ ok1(tdb_transaction_start(tdb) == 0); ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == 0); data = tdb_fetch(tdb, key); ok1(data.dsize == strlen("world")); ok1(memcmp(data.dptr, "world", strlen("world")) == 0); free(data.dptr); ok1(tdb_transaction_commit(tdb) == 0); ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1); tdb_close(tdb); return exit_status(); }
int main(int argc, char *argv[]) { unsigned int i; struct ntdb_context *ntdb; NTDB_DATA key = ntdb_mkdata("key", 3); NTDB_DATA data = ntdb_mkdata("data", 4); int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP, NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT }; plan_tests(sizeof(flags) / sizeof(flags[0]) * 48); for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) { /* RW -> R0 */ ntdb = ntdb_open("run-92-get-set-readonly.ntdb", flags[i]|MAYBE_NOSYNC, O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr); ok1(ntdb); ok1(!(ntdb_get_flags(ntdb) & NTDB_RDONLY)); ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == NTDB_SUCCESS); ntdb_add_flag(ntdb, NTDB_RDONLY); ok1(ntdb_get_flags(ntdb) & NTDB_RDONLY); /* Can't store, append, delete. */ ok1(ntdb_store(ntdb, key, data, NTDB_MODIFY) == NTDB_ERR_RDONLY); ok1(tap_log_messages == 1); ok1(ntdb_append(ntdb, key, data) == NTDB_ERR_RDONLY); ok1(tap_log_messages == 2); ok1(ntdb_delete(ntdb, key) == NTDB_ERR_RDONLY); ok1(tap_log_messages == 3); /* Can't start a transaction, or any write lock. */ ok1(ntdb_transaction_start(ntdb) == NTDB_ERR_RDONLY); ok1(tap_log_messages == 4); ok1(ntdb_chainlock(ntdb, key) == NTDB_ERR_RDONLY); ok1(tap_log_messages == 5); ok1(ntdb_lockall(ntdb) == NTDB_ERR_RDONLY); ok1(tap_log_messages == 6); ok1(ntdb_wipe_all(ntdb) == NTDB_ERR_RDONLY); ok1(tap_log_messages == 7); /* Back to RW. */ ntdb_remove_flag(ntdb, NTDB_RDONLY); ok1(!(ntdb_get_flags(ntdb) & NTDB_RDONLY)); ok1(ntdb_store(ntdb, key, data, NTDB_MODIFY) == NTDB_SUCCESS); ok1(ntdb_append(ntdb, key, data) == NTDB_SUCCESS); ok1(ntdb_delete(ntdb, key) == NTDB_SUCCESS); ok1(ntdb_transaction_start(ntdb) == NTDB_SUCCESS); ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == NTDB_SUCCESS); ok1(ntdb_transaction_commit(ntdb) == NTDB_SUCCESS); ok1(ntdb_chainlock(ntdb, key) == NTDB_SUCCESS); ntdb_chainunlock(ntdb, key); ok1(ntdb_lockall(ntdb) == NTDB_SUCCESS); ntdb_unlockall(ntdb); ok1(ntdb_wipe_all(ntdb) == NTDB_SUCCESS); ok1(tap_log_messages == 7); ntdb_close(ntdb); /* R0 -> RW */ ntdb = ntdb_open("run-92-get-set-readonly.ntdb", flags[i]|MAYBE_NOSYNC, O_RDONLY, 0600, &tap_log_attr); ok1(ntdb); ok1(ntdb_get_flags(ntdb) & NTDB_RDONLY); /* Can't store, append, delete. */ ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == NTDB_ERR_RDONLY); ok1(tap_log_messages == 8); ok1(ntdb_append(ntdb, key, data) == NTDB_ERR_RDONLY); ok1(tap_log_messages == 9); ok1(ntdb_delete(ntdb, key) == NTDB_ERR_RDONLY); ok1(tap_log_messages == 10); /* Can't start a transaction, or any write lock. */ ok1(ntdb_transaction_start(ntdb) == NTDB_ERR_RDONLY); ok1(tap_log_messages == 11); ok1(ntdb_chainlock(ntdb, key) == NTDB_ERR_RDONLY); ok1(tap_log_messages == 12); ok1(ntdb_lockall(ntdb) == NTDB_ERR_RDONLY); ok1(tap_log_messages == 13); ok1(ntdb_wipe_all(ntdb) == NTDB_ERR_RDONLY); ok1(tap_log_messages == 14); /* Can't remove NTDB_RDONLY since we opened with O_RDONLY */ ntdb_remove_flag(ntdb, NTDB_RDONLY); ok1(tap_log_messages == 15); ok1(ntdb_get_flags(ntdb) & NTDB_RDONLY); ntdb_close(ntdb); ok1(tap_log_messages == 15); tap_log_messages = 0; } return exit_status(); }
static void TestExtractParameters() { TCHAR buffer[1024]; const TCHAR *params[64]; unsigned n; // test basic functionality n = WaypointReaderBase::ExtractParameters(_T(""), buffer, params, 64); ok1(n == 1); ok1(_tcscmp(params[0], _T("")) == 0); n = WaypointReaderBase::ExtractParameters(_T("foo"), buffer, params, 64); ok1(n == 1); ok1(_tcscmp(params[0], _T("foo")) == 0); n = WaypointReaderBase::ExtractParameters(_T("foo,bar"), buffer, params, 64); ok1(n == 2); ok1(_tcscmp(params[0], _T("foo")) == 0); ok1(_tcscmp(params[1], _T("bar")) == 0); n = WaypointReaderBase::ExtractParameters(_T("foo,bar"), buffer, params, 1); ok1(n == 1); ok1(_tcscmp(params[0], _T("foo")) == 0); n = WaypointReaderBase::ExtractParameters(_T("foo,bar,"), buffer, params, 64); ok1(n == 3); ok1(_tcscmp(params[0], _T("foo")) == 0); ok1(_tcscmp(params[1], _T("bar")) == 0); ok1(_tcscmp(params[2], _T("")) == 0); n = WaypointReaderBase::ExtractParameters(_T("foo,bar,,"), buffer, params, 64); ok1(n == 4); ok1(_tcscmp(params[0], _T("foo")) == 0); ok1(_tcscmp(params[1], _T("bar")) == 0); ok1(_tcscmp(params[2], _T("")) == 0); ok1(_tcscmp(params[3], _T("")) == 0); // with qoutes but no quote handling n = WaypointReaderBase::ExtractParameters(_T("\"foo,comma\",\"bar\""), buffer, params, 64); ok1(n == 3); ok1(_tcscmp(params[0], _T("\"foo")) == 0); ok1(_tcscmp(params[1], _T("comma\"")) == 0); ok1(_tcscmp(params[2], _T("\"bar\"")) == 0); // quote handling n = WaypointReaderBase::ExtractParameters(_T("\"\""), buffer, params, 64, false, _T('"')); ok1(n == 1); ok1(_tcscmp(params[0], _T("")) == 0); n = WaypointReaderBase::ExtractParameters(_T("\"\"\""), buffer, params, 64, false, _T('"')); ok1(n == 1); ok1(_tcscmp(params[0], _T("\"")) == 0); n = WaypointReaderBase::ExtractParameters(_T("\"\"\"\""), buffer, params, 64, false, _T('"')); ok1(n == 1); ok1(_tcscmp(params[0], _T("\"")) == 0); n = WaypointReaderBase::ExtractParameters(_T("\"foo,comma\",\"bar\""), buffer, params, 64, false, _T('"')); ok1(n == 2); ok1(_tcscmp(params[0], _T("foo,comma")) == 0); ok1(_tcscmp(params[1], _T("bar")) == 0); // no quotes, whitespace removal n = WaypointReaderBase::ExtractParameters(_T("foo bar"), buffer, params, 64, true); ok1(n == 1); ok1(_tcscmp(params[0], _T("foo bar")) == 0); n = WaypointReaderBase::ExtractParameters(_T("foo , bar, baz"), buffer, params, 64, true); ok1(n == 3); ok1(_tcscmp(params[0], _T("foo")) == 0); ok1(_tcscmp(params[1], _T("bar")) == 0); ok1(_tcscmp(params[2], _T("baz")) == 0); n = WaypointReaderBase::ExtractParameters(_T(" foo , bar , baz "), buffer, params, 64, true); ok1(n == 3); ok1(_tcscmp(params[0], _T("foo")) == 0); ok1(_tcscmp(params[1], _T("bar")) == 0); ok1(_tcscmp(params[2], _T("baz")) == 0); n = WaypointReaderBase::ExtractParameters(_T(" foo\" , \" bar \" , \"baz "), buffer, params, 64, true); ok1(n == 3); ok1(_tcscmp(params[0], _T("foo\"")) == 0); ok1(_tcscmp(params[1], _T("\" bar \"")) == 0); ok1(_tcscmp(params[2], _T("\"baz")) == 0); // quote handling, whitespace removal n = WaypointReaderBase::ExtractParameters(_T("\"foo \" , \" bar\", \" baz\""), buffer, params, 64, true, _T('"')); ok1(n == 3); ok1(_tcscmp(params[0], _T("foo ")) == 0); ok1(_tcscmp(params[1], _T(" bar")) == 0); ok1(_tcscmp(params[2], _T(" baz")) == 0); n = WaypointReaderBase::ExtractParameters( _T(" \" foo \" , \" bar \" , \" baz \" "), buffer, params, 64, true, _T('"')); ok1(n == 3); ok1(_tcscmp(params[0], _T(" foo ")) == 0); ok1(_tcscmp(params[1], _T(" bar ")) == 0); ok1(_tcscmp(params[2], _T(" baz ")) == 0); n = WaypointReaderBase::ExtractParameters(_T("\"foo\",\"\",\"bar\""), buffer, params, 64, true, _T('"')); ok1(n == 3); ok1(_tcscmp(params[0], _T("foo")) == 0); ok1(_tcscmp(params[1], _T("")) == 0); ok1(_tcscmp(params[2], _T("bar")) == 0); // missing end quote n = WaypointReaderBase::ExtractParameters(_T("\"foo, bar"), buffer, params, 64, true, _T('"')); ok1(n == 1); ok1(_tcscmp(params[0], _T("foo, bar")) == 0); // embedded quotes and commas n = WaypointReaderBase::ExtractParameters(_T("\"foo, \"bar\"\""), buffer, params, 64, true, _T('"')); ok1(n == 1); ok1(_tcscmp(params[0], _T("foo, \"bar\"")) == 0); n = WaypointReaderBase::ExtractParameters(_T("\"foo, \"\"bar\"\"\""), buffer, params, 64, true, _T('"')); ok1(n == 1); ok1(_tcscmp(params[0], _T("foo, \"bar\"")) == 0); }
static struct io_plan no_timeout(struct io_conn *conn, struct data *d) { ok1(d->state == 1); d->state++; return io_close(); }
static void TestMap() { Profile::Clear(); { int value; ok1(!Profile::Exists("key1")); ok1(!Profile::Get("key1", value)); Profile::Set("key1", 4); ok1(Profile::Exists("key1")); ok1(Profile::Get("key1", value)); ok1(value == 4); } { short value; ok1(!Profile::Get("key2", value)); Profile::Set("key2", 123); ok1(Profile::Get("key2", value)); ok1(value == 123); } { unsigned value; ok1(!Profile::Get("key3", value)); Profile::Set("key3", -42); ok1(Profile::Get("key3", value)); ok1(value == -42u); } { bool value; ok1(!Profile::Get("key4", value)); Profile::Set("key4", true); ok1(Profile::Get("key4", value)); ok1(value); Profile::Set("key4", false); ok1(Profile::Get("key4", value)); ok1(!value); } { fixed value; ok1(!Profile::Get("key5", value)); Profile::Set("key5", fixed(1.337)); ok1(Profile::Get("key5", value)); ok1(equals(value, 1.337)); } }
int main(void) { struct data *d = malloc(sizeof(*d)); struct addrinfo *addrinfo; struct io_listener *l; int fd, status; /* This is how many tests you plan to run */ plan_tests(20); d->state = 0; d->timed_out = false; d->timeout_usec = 100000; fd = make_listen_fd(PORT, &addrinfo); ok1(fd >= 0); l = io_new_listener(fd, init_conn, d); ok1(l); fflush(stdout); if (!fork()) { int i; io_close_listener(l); fd = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol); if (fd < 0) exit(1); if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) exit(2); signal(SIGPIPE, SIG_IGN); usleep(500000); for (i = 0; i < strlen("hellothere"); i++) { if (write(fd, "hellothere" + i, 1) != 1) break; } close(fd); freeaddrinfo(addrinfo); free(d); exit(i); } ok1(io_loop() == d); ok1(d->state == 3); ok1(d->timed_out == true); ok1(wait(&status)); ok1(WIFEXITED(status)); ok1(WEXITSTATUS(status) < sizeof(d->buf)); /* This one shouldn't time out. */ d->state = 0; d->timed_out = false; d->timeout_usec = 500000; fflush(stdout); if (!fork()) { int i; io_close_listener(l); fd = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol); if (fd < 0) exit(1); if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) exit(2); signal(SIGPIPE, SIG_IGN); usleep(100000); for (i = 0; i < strlen("hellothere"); i++) { if (write(fd, "hellothere" + i, 1) != 1) break; } close(fd); freeaddrinfo(addrinfo); free(d); exit(i); } ok1(io_loop() == d); ok1(d->state == 3); ok1(d->timed_out == false); ok1(wait(&status)); ok1(WIFEXITED(status)); ok1(WEXITSTATUS(status) >= sizeof(d->buf)); io_close_listener(l); freeaddrinfo(addrinfo); free(d); /* This exits depending on whether all tests passed */ return exit_status(); }
static void CheckLeg(const TaskWaypoint &tp, const AircraftState &aircraft, const TaskStats &stats) { const GeoPoint destination = tp.GetWaypoint().location; const fixed safety_height = GetSafetyHeight(tp); const fixed min_arrival_alt = tp.GetWaypoint().elevation + safety_height; const GeoVector vector = aircraft.location.DistanceBearing(destination); const fixed ld = glide_polar.GetBestLD(); const fixed height_above_min = aircraft.altitude - min_arrival_alt; const fixed height_consumption = vector.distance / ld; const ElementStat &leg = stats.current_leg; const GlideResult &solution_remaining = leg.solution_remaining; ok1(leg.vector_remaining.IsValid()); ok1(equals(leg.vector_remaining.distance, vector.distance)); ok1(equals(leg.vector_remaining.bearing, vector.bearing)); ok1(solution_remaining.IsOk()); ok1(solution_remaining.vector.IsValid()); ok1(equals(solution_remaining.vector.distance, vector.distance)); ok1(equals(solution_remaining.vector.bearing, vector.bearing)); ok1(equals(solution_remaining.height_glide, height_consumption)); ok1(equals(solution_remaining.altitude_difference, height_above_min - height_consumption)); ok1(equals(solution_remaining.GetRequiredAltitudeWithDrift(), min_arrival_alt + height_consumption)); if (height_above_min >= height_consumption) { /* straight glide */ ok1(equals(solution_remaining.height_climb, 0)); } else if (positive(glide_polar.GetMC())) { /* climb required */ ok1(equals(solution_remaining.height_climb, height_consumption - height_above_min)); } else { /* climb required, but not possible (MC=0) */ ok1(equals(solution_remaining.height_climb, 0)); } }