1 /**
2  * Internal functions used in unittests.
3  */
4 module during.tests.base;
5 
6 import core.sys.linux.fcntl;
7 
8 package:
9 
10 version (D_BetterC) string errmsg; // used to pass error texts on tests that can't be completed
11 
12 auto getTestFileName(string baseName)()
13 {
14     import core.stdc.stdlib : rand, srand;
15     import core.sys.posix.pthread : pthread_self;
16     import core.sys.posix.time : clock_gettime, CLOCK_REALTIME, timespec;
17 
18     // make rand a bit more random - nothing fancy needed
19     timespec t;
20     auto tr = clock_gettime(CLOCK_REALTIME, &t);
21     assert(tr == 0);
22     srand(cast(uint)(t.tv_nsec * pthread_self()));
23 
24     static immutable ubyte[] let = cast(immutable(ubyte[]))"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
25     char[baseName.length + 16] fname = baseName ~ "_**********.dat\0";
26 
27     foreach (i; 0..10)
28     {
29         fname[baseName.length + 1 + i] = let[rand() % let.length];
30     }
31     return fname;
32 }
33 
34 auto openFile(T)(T fname, int flags)
35 {
36     auto f = open(&fname[0], flags, 0x1a4); //0644 (std.conv.octal doesn't work with betterC)
37     assert(f >= 0, "Failed to open file");
38     return f;
39 }