Directory: | ./ |
---|---|
File: | TESTS/TEST_LAUCH_PARALLEL_THREAD/main.cpp |
Date: | 2025-05-16 18:34:22 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 92 | 92 | 100.0% |
Branches: | 155 | 155 | 100.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | |||
2 | /*************************************** | ||
3 | Auteur : Pierre Aubert | ||
4 | Mail : pierre.aubert@lapp.in2p3.fr | ||
5 | Licence : CeCILL-C | ||
6 | ****************************************/ | ||
7 | |||
8 | #include <iostream> | ||
9 | #include <fstream> | ||
10 | #include "phoenix_assert.h" | ||
11 | #include "phoenix_check.h" | ||
12 | #include "lauchParallelThread.h" | ||
13 | |||
14 | ///Process a list of files | ||
15 | /** @param listFile : list of the files to be processed | ||
16 | * @param scal : scalar value | ||
17 | */ | ||
18 | 10 | void processFiles(const std::vector<std::string> & listFile, float scal){ | |
19 |
2/2✓ Branch 4 taken 64 times.
✓ Branch 5 taken 10 times.
|
74 | for(std::vector<std::string>::const_iterator it(listFile.begin()); it != listFile.end(); ++it){ |
20 | 64 | float res(0.0f); | |
21 |
2/2✓ Branch 0 taken 478610 times.
✓ Branch 1 taken 64 times.
|
478674 | for(long unsigned int i(1lu); i < 10000lu; ++i){ |
22 | 478610 | res += 1.0f/(((float)i)*((float)i)) - scal/((float)i); | |
23 | } | ||
24 |
1/1✓ Branch 1 taken 64 times.
|
64 | std::ofstream fs; |
25 |
1/1✓ Branch 3 taken 64 times.
|
64 | fs.open(it->c_str()); |
26 | |||
27 |
2/2✓ Branch 1 taken 64 times.
✓ Branch 4 taken 64 times.
|
64 | fs << res << std::endl; |
28 |
1/1✓ Branch 1 taken 64 times.
|
64 | fs.close(); |
29 | 64 | } | |
30 | 10 | } | |
31 | |||
32 | ///Test the lauchParallelThread function | ||
33 | 1 | void testLauchParallelThread(){ | |
34 | 1 | std::vector<std::string> listFile; | |
35 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | listFile.push_back("file1.txt"); |
36 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | listFile.push_back("file2.txt"); |
37 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | listFile.push_back("file3.txt"); |
38 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | listFile.push_back("file4.txt"); |
39 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | listFile.push_back("file5.txt"); |
40 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | listFile.push_back("file6.txt"); |
41 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | listFile.push_back("file7.txt"); |
42 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | listFile.push_back("file8.txt"); |
43 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | listFile.push_back("file9.txt"); |
44 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | listFile.push_back("file10.txt"); |
45 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | listFile.push_back("file11.txt"); |
46 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | listFile.push_back("file12.txt"); |
47 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | listFile.push_back("file13.txt"); |
48 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | listFile.push_back("file14.txt"); |
49 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | listFile.push_back("file15.txt"); |
50 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | listFile.push_back("file16.txt"); |
51 | |||
52 | 1 | float scal(1.0f); | |
53 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | for(size_t i(0lu); i < 5lu; ++i){ |
54 |
1/1✓ Branch 1 taken 5 times.
|
5 | lauchParallelThread(listFile, i, processFiles, scal); |
55 | } | ||
56 | 1 | } | |
57 | |||
58 | ///Process a list of files | ||
59 | /** @param fs : log output to be used | ||
60 | * @param listFile : list of the files to be processed | ||
61 | * @param scal : scalar value | ||
62 | */ | ||
63 | 20 | void processFilesLog(std::ofstream & fs, const std::vector<PPath> & listFile, float scal){ | |
64 |
2/2✓ Branch 4 taken 128 times.
✓ Branch 5 taken 20 times.
|
148 | for(std::vector<PPath>::const_iterator it(listFile.begin()); it != listFile.end(); ++it){ |
65 | 128 | float res(0.0f); | |
66 |
2/2✓ Branch 0 taken 1026212 times.
✓ Branch 1 taken 128 times.
|
1026340 | for(long unsigned int i(1lu); i < 10000lu; ++i){ |
67 | 1026212 | res += 1.0f/(((float)i)*((float)i)) - scal/((float)i); | |
68 | } | ||
69 |
1/1✓ Branch 1 taken 128 times.
|
128 | std::ofstream fsOut; |
70 |
1/1✓ Branch 2 taken 128 times.
|
128 | fsOut.open(*it); |
71 | |||
72 |
2/2✓ Branch 1 taken 128 times.
✓ Branch 4 taken 128 times.
|
128 | fsOut << res << std::endl; |
73 |
4/4✓ Branch 1 taken 128 times.
✓ Branch 5 taken 128 times.
✓ Branch 8 taken 128 times.
✓ Branch 11 taken 128 times.
|
128 | fs << "processFilesLog : process file '"<<(*it)<<"'" << std::endl; |
74 |
1/1✓ Branch 1 taken 128 times.
|
128 | fsOut.close(); |
75 | 128 | } | |
76 | 20 | } | |
77 | |||
78 | ///Check the function lauchParallelThreadLogList with files | ||
79 | /** @param logFileName : name of the log file to be used | ||
80 | * @param expectedOk : true if we expect the function to work | ||
81 | * @return true on success, false otherwise | ||
82 | */ | ||
83 | 3 | bool testLauchParallelThreadLogListFile(const PPath & logFileName, bool expectedOk){ | |
84 | 3 | std::vector<PPath> listFile; | |
85 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | listFile.push_back(PPath("file1.txt")); |
86 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | listFile.push_back(PPath("file2.txt")); |
87 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | listFile.push_back(PPath("file3.txt")); |
88 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | listFile.push_back(PPath("file4.txt")); |
89 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | listFile.push_back(PPath("file5.txt")); |
90 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | listFile.push_back(PPath("file6.txt")); |
91 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | listFile.push_back(PPath("file7.txt")); |
92 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | listFile.push_back(PPath("file8.txt")); |
93 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | listFile.push_back(PPath("file9.txt")); |
94 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | listFile.push_back(PPath("file10.txt")); |
95 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | listFile.push_back(PPath("file11.txt")); |
96 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | listFile.push_back(PPath("file12.txt")); |
97 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | listFile.push_back(PPath("file13.txt")); |
98 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | listFile.push_back(PPath("file14.txt")); |
99 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | listFile.push_back(PPath("file15.txt")); |
100 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | listFile.push_back(PPath("file16.txt")); |
101 | |||
102 | 3 | bool b(true); | |
103 | 3 | float scal(1.0f); | |
104 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3 times.
|
18 | for(size_t i(0lu); i < 5lu; ++i){ |
105 |
1/1✓ Branch 1 taken 15 times.
|
15 | b &= lauchParallelThreadLog(listFile, logFileName, i, processFilesLog, scal); |
106 | } | ||
107 | 3 | bool isSuccess(b == expectedOk); | |
108 | // phoenix_functionOk("testLauchParallelThreadLogListFile("+logFileName+")", isSuccess); | ||
109 | 3 | return isSuccess; | |
110 | 3 | } | |
111 | |||
112 | ///Write some log | ||
113 | /** @param fs : log output to be used | ||
114 | * @param scal : scalar value | ||
115 | */ | ||
116 | 20 | void processFilesLogOnly(std::ofstream & fs, float scal){ | |
117 | 20 | float res(0.0f); | |
118 |
2/2✓ Branch 0 taken 1980 times.
✓ Branch 1 taken 20 times.
|
2000 | for(long unsigned int i(1lu); i < 100lu; ++i){ |
119 | 1980 | res += 1.0f/(((float)i)*((float)i)) - scal/((float)i); | |
120 | 1980 | fs << "processFilesLogOnly : res["<<i<<"] '"<<res<<"'" << std::endl; | |
121 | } | ||
122 | 20 | } | |
123 | |||
124 | ///Check the function lauchParallelThreadLogList with log only | ||
125 | /** @param logFileName : name of the log file to be used | ||
126 | * @param expectedOk : true if we expect the function to work | ||
127 | * @return true on success, false otherwise | ||
128 | */ | ||
129 | 3 | bool testLauchParallelThreadLogOnlyLog(const PPath & logFileName, bool expectedOk){ | |
130 | 3 | bool b(true); | |
131 | 3 | float scal(1.0f); | |
132 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3 times.
|
18 | for(size_t i(0lu); i < 5lu; ++i){ |
133 |
1/1✓ Branch 1 taken 15 times.
|
15 | b &= lauchParallelThreadLog(logFileName, i, processFilesLogOnly, scal); |
134 | } | ||
135 | 3 | bool isSuccess(b == expectedOk); | |
136 | // phoenix_functionOk("testLauchParallelThreadLogOnlyLog("+logFileName+")", isSuccess); | ||
137 | 3 | return isSuccess; | |
138 | } | ||
139 | |||
140 | 1 | int main(int argc, char** argv){ | |
141 | 1 | testLauchParallelThread(); | |
142 |
7/7✓ Branch 2 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 16 taken 1 times.
✓ Branch 19 taken 1 times.
✓ Branch 22 taken 1 times.
|
1 | phoenix_assert(testLauchParallelThreadLogListFile(PPath("basic_log_file.txt"), true)); |
143 |
7/7✓ Branch 2 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 16 taken 1 times.
✓ Branch 19 taken 1 times.
✓ Branch 22 taken 1 times.
|
1 | phoenix_assert(testLauchParallelThreadLogListFile(PPath("basic_log_file"), true)); |
144 |
7/7✓ Branch 2 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 16 taken 1 times.
✓ Branch 19 taken 1 times.
✓ Branch 22 taken 1 times.
|
1 | phoenix_assert(testLauchParallelThreadLogListFile(PPath("some/inexisting/logFile.txt"), false)); |
145 | |||
146 |
7/7✓ Branch 2 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 16 taken 1 times.
✓ Branch 19 taken 1 times.
✓ Branch 22 taken 1 times.
|
1 | phoenix_assert(testLauchParallelThreadLogOnlyLog(PPath("only_log_file.txt"), true)); |
147 |
7/7✓ Branch 2 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 16 taken 1 times.
✓ Branch 19 taken 1 times.
✓ Branch 22 taken 1 times.
|
1 | phoenix_assert(testLauchParallelThreadLogOnlyLog(PPath("only_log_file"), true)); |
148 |
7/7✓ Branch 2 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 16 taken 1 times.
✓ Branch 19 taken 1 times.
✓ Branch 22 taken 1 times.
|
1 | phoenix_assert(testLauchParallelThreadLogOnlyLog(PPath("some/inexisting/logFile.txt"), false)); |
149 | 1 | return 0; | |
150 | } | ||
151 | |||
152 | |||
153 |