Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[6model/c] get current time to microsecond resolution on unix and win32

  • Loading branch information...
commit ed14a4353bae723247abe56ccff44707071c8f51 1 parent 2795df3
@mberends mberends authored
View
74 c/t/01-toolchain/01b-timing.c
@@ -0,0 +1,74 @@
+/* 01b-timing.c */
+
+#include <stdio.h> /* sprintf */
+#ifdef _WIN32
+ #include <windows.h>
+#else
+ #include <sys/time.h>
+#endif
+#include "../Test.h" /* is_ii plan */
+
+
+/* seconds_microseconds_sleep */
+void
+seconds_microseconds_sleep()
+{
+ /* long long for 64 bits because on Win32, long is only 32 bits */
+ long long seconds1, seconds2, seconds3, timediff;
+ int microseconds1, microseconds2, microseconds3;
+ char message[80];
+
+ /* Read the clock twice in quick succession, sleep for 1 second, */
+ /* then read the clock a third time. Report the differences */
+ /* between the times and verify that the sleep was about 1 sec. */
+ #ifdef _WIN32
+ FILETIME time1, time2, time3;
+ GetSystemTimeAsFileTime(&time1); /* * 100ns since 1601-01-01 */
+ GetSystemTimeAsFileTime(&time2);
+ Sleep(1000); /* milliseconds */
+ GetSystemTimeAsFileTime(&time3);
+ seconds1 = (((long long)time1.dwHighDateTime) << 32) | time1.dwLowDateTime;
+ microseconds1 = (seconds1 % 10000000)/10; seconds1 /= 10000000;
+ seconds2 = (((long long)time2.dwHighDateTime) << 32) | time2.dwLowDateTime;
+ microseconds2 = (seconds2 % 10000000)/10; seconds2 /= 10000000;
+ seconds3 = (((long long)time3.dwHighDateTime) << 32) | time3.dwLowDateTime;
+ microseconds3 = (seconds3 % 10000000)/10; seconds3 /= 10000000;
+ #else
+ struct timeval time1, time2, time3;
+ gettimeofday(&time1, NULL);
+ gettimeofday(&time2, NULL);
+ sleep(1); /* seconds */
+ gettimeofday(&time3, NULL);
+ seconds1 = time1.tv_sec; microseconds1 = time1.tv_usec;
+ seconds2 = time2.tv_sec; microseconds2 = time2.tv_usec;
+ seconds3 = time3.tv_sec; microseconds3 = time3.tv_usec;
+ #endif
+ /* test 1 - time1 is nonzero */
+ sprintf(message, "clock returned %lld seconds and %d microseconds",
+ seconds1, microseconds1);
+ ok(seconds1 > 0L && microseconds1 > 0, message);
+
+ /* test 2 - time2 >= time1 */
+ sprintf(message, "clock returned %lld seconds and %d microseconds",
+ seconds2, microseconds2);
+ ok(seconds2 > seconds1 || microseconds2 >= microseconds1, message);
+
+ /* test 3 - time3 is approximately time2 + 1000000 microseconds */
+ timediff = (seconds3 - seconds2) * 1000000
+ + microseconds3 - microseconds2;
+ sprintf(message, "one second sleep measured %lld microseconds",
+ timediff);
+ ok(999000 < timediff && timediff < 1000999, message);
+}
+
+
+/* main */
+int
+main(int arg, char * argv[])
+{
+ plan(3);
+ seconds_microseconds_sleep();
+ return 0;
+}
+
+/* end of 01b-timing.c */
View
0  c/t/01-toolchain/01b-threads.c → c/t/01-toolchain/01c-threads.c
File renamed without changes
View
15 c/t/Test.h
@@ -21,28 +21,33 @@ main(int argc, char * argv[])
*/
-#include <stdio.h>
+#include <stdio.h> /* printf */
-int testnumber=0; /* yes, namespace pollution. patches welcome ;-) */
+int _test_number=0; /* yes, namespace pollution. patches welcome ;-) */
#define plan(count) printf("1..%d\n", count)
#define \
ok(flag,desc) \
printf("%sok %d - %s\n", \
- flag?"":"not ",++testnumber,desc)
+ flag?"":"not ",++_test_number,desc)
#define \
is_ii(got,expected,desc) \
printf("%sok %d - %s\n", \
- got==expected?"":"not ",++testnumber,desc); \
+ got==expected?"":"not ",++_test_number,desc); \
if(got!=expected) \
printf("# got : %d\n# expected : %d\n", got, expected)
#define \
is_ss(got,expected,desc) \
printf("%sok %d - %s\n", \
- strcmp(got,expected)?"not ":"",++testnumber,desc)
+ strcmp(got,expected)?"not ":"",++_test_number,desc)
+
+#define \
+isnt_pp(got,expected,desc) \
+ printf("%sok %d - %s\n", \
+ (got!=expected)?"":"not ", ++_test_number, desc)
#define \
diag(message) \
View
27 c/tools/build/Makefile.in
@@ -4,9 +4,9 @@
# to compile and run 6model/c/tools/build/Configure.c which reads from
# 6model/c/tools/build/Makefile.in and generates 6model/c/Makefile.
-# Targets that do not produce files (tells make not to waste time
-# checking that such files exist).
-.PHONY: all clean test test01 test02
+# Targets that do not produce files (tells the make utility to skip
+# looking for such non existent files).
+.PHONY: all clean realclean test test01 test02
# The compiled Configure.c will replace specific words between @ signs.
CC = @cc@
@@ -23,26 +23,31 @@ t/01-toolchain/01a-cc.exe: t/01-toolchain/01a-cc.c t/Test.h
$(CC) $(O)t/01-toolchain/01a-cc.exe t/01-toolchain/01a-cc.c
-$(RM_RF) 01a-cc.obj
-t/01-toolchain/01b-threads.exe: t/01-toolchain/01b-threads.c t/Test.h
- $(CC) -pthread $(O)t/01-toolchain/01b-threads.exe t/01-toolchain/01b-threads.c
- -$(RM_RF) 01b-threads.obj
+t/01-toolchain/01b-timing.exe: t/01-toolchain/01b-timing.c t/Test.h
+ $(CC) $(O)t/01-toolchain/01b-timing.exe t/01-toolchain/01b-timing.c
+ -$(RM_RF) 01b-timing.obj
+
+t/01-toolchain/01c-threads.exe: t/01-toolchain/01c-threads.c t/Test.h
+ $(CC) -pthread $(O)t/01-toolchain/01c-threads.exe t/01-toolchain/01c-threads.c
+ -$(RM_RF) 01c-threads.obj
t/02-components/02a-hashtable.exe: t/02-components/02a-hashtable.c \
src/hashtable.h src/hashtable.c t/Test.h
$(CC) $(O)t/02-components/02a-hashtable.exe src/hashtable.c t/02-components/02a-hashtable.c
- -$(RM_RF) hashtable.obj
+ -$(RM_RF) hashtable.obj 02a-hashtable.obj
t/02-components/02b-heapmanager.exe: t/02-components/02b-heapmanager.c \
src/heapmanager.h src/heapmanager.c t/Test.h
$(CC) $(O)t/02-components/02b-heapmanager.exe src/heapmanager.c t/02-components/02b-heapmanager.c
- -$(RM_RF) hashtable.obj
+ -$(RM_RF) heapmanager.obj 02b-heapmanager.obj
tools/build/prove$(EXE): tools/build/prove.c
$(CC) $(O)tools/build/prove$(EXE) tools/build/prove.c
-$(RM_RF) prove.obj
# Test executables are named *.exe even on Unix so that prove can find them
-test01: t/01-toolchain/01a-cc.exe tools/build/prove$(EXE)
+test01: t/01-toolchain/01a-cc.exe t/01-toolchain/01b-timing.exe \
+ t/01-toolchain/01c-threads.exe tools/build/prove$(EXE)
tools/build/prove -e "" --ext ".exe" t/01-toolchain
test02: t/02-components/02a-hashtable.exe tools/build/prove$(EXE)
@@ -52,7 +57,9 @@ test: test01 test02
# Miscellaneous targets
clean:
- $(RM_RF) *.exe *.obj *.dll *.lib *.exp
+ $(RM_RF) *.exe *.obj *.dll *.lib *.exp \
+ tools/build/Configure$(EXE) tools/build/prove$(EXE) \
+ t/01-toolchain/*.exe t/01-components/*.exe
realclean: clean
perl -MExtUtils::Command -e rm_f Makefile
Please sign in to comment.
Something went wrong with that request. Please try again.