Example #1
0
MojErr MojTimeTest::run()
{
	MojTimevalT tv;
	tv.tv_sec = 400;
	tv.tv_usec = 54321;
	MojTime time = -8;
	time.fromTimeval(&tv);
	MojTestAssert(time == 400054321);
	MojTestAssert(time.secs() == 400);
	MojTestAssert(time.millisecs() == 400054);
	MojTestAssert(time.microsecs() == 400054321);
	MojTestAssert(time.millisecsPart() == 54);
	MojTestAssert(time.microsecsPart() == 54321);
	MojTimevalT tv2;
	time.toTimeval(&tv2);
	MojTestAssert(tv.tv_sec == tv2.tv_sec && tv.tv_usec == tv2.tv_usec);
	MojTimespecT ts;
	ts.tv_sec = 400;
	ts.tv_nsec = 54321;
	time.fromTimespec(&ts);
	MojTestAssert(time == 400000054);
	MojTimespecT ts2;
	time.toTimespec(&ts2);
	MojTestAssert(ts2.tv_sec = 400 && ts2.tv_nsec == 54000);

	time = MojSecs(3);
	MojTestAssert(time == 3000000);
	time = MojMillisecs(45);
	MojTestAssert(time == 45000);
	time = MojMicrosecs(5);
	MojTestAssert(time == 5);
	time = MojSecs(1) + MojMillisecs(2) + MojMicrosecs(3);
	MojTestAssert(time == 1002003);

	time = 1;
	MojTestAssert(time++ == 1);
	MojTestAssert(time == 2);
	MojTestAssert(++time == 3);
	MojTestAssert(--time == 2);
	MojTestAssert(time-- == 2);
	MojTestAssert(time == 1);
	MojTestAssert((time += 2) == 3);
	MojTestAssert((time -= 4) == -1);
	MojTestAssert((time *= -10) == 10);
	MojTestAssert((time /= 2) == 5);
	time = MojTime(1) + MojTime(2);
	MojTestAssert(time == 3);
	time = MojTime(8) - MojTime(6);
	MojTestAssert(time == 2);
	time = MojTime(8) * MojTime(6);
	MojTestAssert(time == 48);
	time = MojTime(8) / MojTime(4);
	MojTestAssert(time == 2);

	return MojErrNone;
}
Example #2
0
MojErr MojThreadTest::basicTest()
{
	MojVector<MojThreadT> threads;
	MojThreadTestArgs args;
	for (int i = 0; i < MojTestNumThreads; ++i) {
		MojThreadT thread = MojInvalidThread;
		MojErr err = MojThreadCreate(thread, MojThreadTestFn, &args);
		MojTestErrCheck(err);
		MojTestAssert(thread != MojInvalidThread);
		err = threads.push(thread);
		MojTestErrCheck(err);
	}

	{
		MojThreadGuard guard(args.m_mutex);
		while (args.m_counter < (MojTestNumThreads * MojTestNumIterations)) {
			MojErr err = args.m_countCond.wait(args.m_mutex);
			MojErrCheck(err);
		}
		MojTestAssert(args.m_counter == (MojTestNumThreads * MojTestNumIterations));
		MojTestAssert(args.m_atomicCounter == (MojTestNumThreads * MojTestNumIterations));
		guard.unlock();
		MojErr err = MojSleep(MojMillisecs(500));
		MojTestErrCheck(err);
		guard.lock();
		args.m_wait = false;
		err = args.m_waitCond.broadcast();
		MojTestErrCheck(err);
	}

	for (MojVector<MojThreadT>::ConstIterator i = threads.begin();
		 i != threads.end();
		 ++i) {
		MojErr threadErr = MojErrNone;
		MojErr err = MojThreadJoin(*i, threadErr);
		MojTestErrCheck(err);
		MojTestErrCheck(threadErr);
	}
	MojTestAssert(args.m_counter == 0);
	MojTestAssert(args.m_atomicCounter == 0);

	return MojErrNone;
}