Directory: | ./ |
---|---|
File: | TESTS/TEST_MULTITHREAD_PROGRESS_BAR/main_full_multithread_progress.cpp |
Date: | 2025-05-16 18:34:22 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 32 | 33 | 97.0% |
Branches: | 35 | 41 | 85.4% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #include <iostream> | ||
8 | #include <string> | ||
9 | #include <chrono> | ||
10 | #include <thread> | ||
11 | #include <sstream> | ||
12 | #include <vector> | ||
13 | |||
14 | #include "PMultiThreadProgress.h" | ||
15 | |||
16 | ///Fake data computing | ||
17 | /** @param progress : multiple progress bar | ||
18 | * @param vecComputeData : vector of compute data | ||
19 | * @param index : index of data to be computed | ||
20 | */ | ||
21 | 1 | void fake_compute_data(PMultiThreadProgress & progress, const std::vector<int> & vecComputeData, size_t index){ | |
22 |
1/1✓ Branch 1 taken 1 times.
|
1 | std::stringstream name; |
23 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
|
1 | name << "Some computing of data index " << index; |
24 | //Start the progress bar | ||
25 |
3/3✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 8 taken 1 times.
|
1 | size_t indexProgressBar = progress.addProgressBar(name.str(), vecComputeData[index]); |
26 | //Simulate computation which depends on input data | ||
27 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
|
4 | for(int i(0); i < vecComputeData[index]; ++i){ //Simulate some avancement |
28 |
2/6✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
|
3 | if(vecComputeData[index] == 5 && i == 3){ |
29 | ✗ | progress.setError(index); | |
30 | } | ||
31 |
1/1✓ Branch 1 taken 3 times.
|
3 | progress.incrementProgress(indexProgressBar); //Update the progress bar |
32 |
1/1✓ Branch 2 taken 3 times.
|
3 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); //Simulate computation |
33 | } | ||
34 | // std::this_thread::sleep_for(std::chrono::seconds(vecComputeData[index])); | ||
35 | //Say the computation is finished | ||
36 |
1/1✓ Branch 1 taken 1 times.
|
1 | progress.incrementProgress(indexProgressBar); |
37 | 1 | } | |
38 | |||
39 | 1 | int main(){ | |
40 | 1 | size_t nbValue(1lu); | |
41 | 1 | std::vector<int> vecComputeData; | |
42 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for(size_t i(0lu); i < nbValue; ++i){ |
43 |
1/1✓ Branch 1 taken 1 times.
|
1 | vecComputeData.push_back(3lu + ((i*11lu)%5lu)); |
44 | } | ||
45 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
|
1 | std::cout << "Start computation" << std::endl; |
46 | |||
47 |
1/1✓ Branch 1 taken 1 times.
|
1 | PMultiThreadProgress progress(nbValue); |
48 | |||
49 | 1 | std::vector<std::thread> vecThread; | |
50 |
1/1✓ Branch 1 taken 1 times.
|
1 | vecThread.resize(nbValue); |
51 | |||
52 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::thread threadProgress(phoenix_print_parallel_progress, std::ref(progress), 1); |
53 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for(size_t i(0lu); i < nbValue; ++i){ |
54 |
1/1✓ Branch 2 taken 1 times.
|
1 | vecThread[i] = std::thread(fake_compute_data, std::ref(progress), vecComputeData, i); |
55 | } | ||
56 |
1/1✓ Branch 1 taken 1 times.
|
1 | threadProgress.join(); |
57 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for(size_t i(0lu); i < nbValue; ++i){ |
58 |
1/1✓ Branch 2 taken 1 times.
|
1 | vecThread[i].join(); |
59 | } | ||
60 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
|
1 | PMultiThreadProgress progressCopy(progress), progressEqual; |
61 |
1/1✓ Branch 1 taken 1 times.
|
1 | progressEqual = progress; |
62 |
1/1✓ Branch 1 taken 1 times.
|
1 | progressCopy.setNbExpectedProgressBar(0lu); |
63 |
1/1✓ Branch 1 taken 1 times.
|
1 | progressEqual.setNbExpectedProgressBar(0lu); |
64 | |||
65 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
|
1 | std::cout << "Done" << std::endl; |
66 | 1 | return 0; | |
67 | 1 | } | |
68 | |||
69 | |||
70 |