Directory: | ./ |
---|---|
File: | tmp_project/PhoenixTex2Html/tmp_project/PhoenixUnitTest/program/main.cpp |
Date: | 2025-03-24 18:12:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 76 | 78 | 97.4% |
Branches: | 128 | 141 | 90.8% |
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 <stdlib.h> | ||
9 | #include <fstream> | ||
10 | #include <iostream> | ||
11 | |||
12 | #include "OptionParser.h" | ||
13 | #include "PFileParser.h" | ||
14 | |||
15 | ///Create the OptionParser of this program | ||
16 | /** @return OptionParser of this program | ||
17 | */ | ||
18 | 10 | OptionParser createOptionParser(){ | |
19 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
|
10 | OptionParser parser(true, __PROGRAM_VERSION__); |
20 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
|
10 | parser.setExampleLongOption("phoenix_dripfile --input=fileInput.txt --command=\"some_program FILENAME\""); |
21 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
|
10 | parser.setExampleShortOption("phoenix_dripfile -i fileInput1.txt -c \"some_program FILENAME\""); |
22 | |||
23 |
4/4✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
✓ Branch 10 taken 10 times.
|
10 | parser.addOption("input", "i", OptionType::FILENAME, true, "list of input files"); |
24 |
4/4✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
✓ Branch 10 taken 10 times.
|
10 | parser.addOption("command", "c", OptionType::STRING, true, "Command to be executed by the program. The token 'FILENAME' specifies where the generated file has to be passed in the command"); |
25 | |||
26 | 10 | size_t firstToken(0lu), lastToken(-1lu), incrementToken(1lu); | |
27 |
4/4✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
✓ Branch 10 taken 10 times.
|
10 | parser.addOption("firsttoken", "f", firstToken, "Index of the first token to be tested"); |
28 |
4/4✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
✓ Branch 10 taken 10 times.
|
10 | parser.addOption("lasttoken", "l", lastToken, "Index of the last token to be tested"); |
29 |
4/4✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
✓ Branch 10 taken 10 times.
|
10 | parser.addOption("incrementtoken", "k", incrementToken, "Increment of the index to be used"); |
30 | |||
31 |
1/1✓ Branch 1 taken 10 times.
|
10 | PString delimitor("+-*/!:~&|<>.()[]{}\"'%"); |
32 |
5/5✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
✓ Branch 10 taken 10 times.
✓ Branch 13 taken 10 times.
|
10 | parser.addOption("delimitor", "d", delimitor, "All delimitor characters to be used to tokenise the input file"); |
33 | |||
34 |
4/4✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
✓ Branch 10 taken 10 times.
|
10 | parser.addOption("nofail", "x", OptionType::NONE, false, "Does not take account if the tested program failed or not. Always retruns 0"); |
35 | |||
36 | 20 | return parser; | |
37 | 10 | } | |
38 | |||
39 | ///Process the file | ||
40 | /** @param inputFile : input file | ||
41 | * @param command : command to be used | ||
42 | * @param firstToken : Index of the first token to be tested | ||
43 | * @param lastToken : Index of the last token to be tested | ||
44 | * @param incrementToken : Increment of the index to be used | ||
45 | * @param delimitor : All delimitor characters to be used to tokenise the input file | ||
46 | * @param isNoFail : true to ignore if the tested program failed or not, false for normal behaviour | ||
47 | * @return true on success, false otherwise | ||
48 | */ | ||
49 | 10 | bool processFile(const PPath & inputFile, const PString & command, | |
50 | size_t firstToken, size_t lastToken, size_t incrementToken, const PString & delimitor, bool isNoFail) | ||
51 | { | ||
52 |
4/4✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
✓ Branch 10 taken 10 times.
|
10 | std::cout << "processFile : using file '"<<inputFile<<"'" << std::endl; |
53 |
1/1✓ Branch 1 taken 10 times.
|
10 | PString extention(inputFile.getExtension()); |
54 |
4/5✓ Branch 1 taken 10 times.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 10 times.
✓ Branch 9 taken 10 times.
|
10 | if(extention != ""){extention = "." + extention;} |
55 |
3/3✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
|
20 | PPath configFile("temporary_file" + extention); |
56 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
|
10 | PString updatedCommand(command.replace("FILENAME", configFile)); |
57 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 9 times.
|
10 | if(command == updatedCommand){ |
58 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
|
1 | std::cerr << "processFile : 'FILENAME' token missing in the command" << std::endl; |
59 |
3/3✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
|
1 | std::cerr << "\t" << command << std::endl; |
60 | 1 | return false; | |
61 | } | ||
62 |
4/4✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
✓ Branch 7 taken 9 times.
✓ Branch 10 taken 9 times.
|
9 | std::cout << "Command : '"<<updatedCommand<<"'" << std::endl; |
63 | |||
64 |
1/1✓ Branch 1 taken 9 times.
|
9 | PFileParser parser; |
65 |
1/1✓ Branch 1 taken 9 times.
|
9 | parser.setSeparator(delimitor); |
66 |
2/3✓ Branch 1 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
|
9 | if(!parser.open(inputFile)){return false;} |
67 | |||
68 | 9 | bool b(true); | |
69 | 9 | size_t tokenIndex(firstToken); | |
70 |
1/1✓ Branch 1 taken 9 times.
|
9 | PString body(""); |
71 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 9 times.
|
69 | for(size_t i(0lu); i < firstToken; ++i){ //Let's create the basic configuration file with the first number of token |
72 |
1/1✓ Branch 1 taken 60 times.
|
60 | PString skippedStr(""); |
73 |
1/1✓ Branch 1 taken 60 times.
|
60 | PString token(parser.getNextToken(skippedStr)); |
74 |
2/2✓ Branch 1 taken 60 times.
✓ Branch 4 taken 60 times.
|
60 | body += skippedStr + token; |
75 | 60 | } | |
76 |
7/7✓ Branch 0 taken 216 times.
✓ Branch 1 taken 5 times.
✓ Branch 3 taken 216 times.
✓ Branch 5 taken 213 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 213 times.
✓ Branch 8 taken 8 times.
|
221 | while(tokenIndex < lastToken && !parser.isEndOfFile()){ |
77 | //Let's create the config file | ||
78 |
2/3✓ Branch 1 taken 213 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 213 times.
|
213 | if(!configFile.saveFileContent(body)){ |
79 | ✗ | std::cerr << "processFile : cannot save file '"<<configFile<<"'" << std::endl; | |
80 | ✗ | return false; | |
81 | } | ||
82 | //Let's call the updatedCommand | ||
83 |
3/3✓ Branch 1 taken 213 times.
✓ Branch 4 taken 213 times.
✓ Branch 7 taken 213 times.
|
213 | std::cout << "- token " << tokenIndex << " => "; |
84 |
7/7✓ Branch 2 taken 213 times.
✓ Branch 4 taken 69 times.
✓ Branch 5 taken 144 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 68 times.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 212 times.
|
213 | if(system(updatedCommand.c_str()) != 0 && !isNoFail){ |
85 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
|
1 | std::cout << "FAIL" << std::endl; |
86 | 1 | return false; | |
87 | } | ||
88 |
2/2✓ Branch 1 taken 212 times.
✓ Branch 4 taken 212 times.
|
212 | std::cout << "OK" << std::endl; |
89 |
2/2✓ Branch 0 taken 948 times.
✓ Branch 1 taken 212 times.
|
1160 | for(size_t i(0lu); i < incrementToken; ++i){ |
90 |
1/1✓ Branch 1 taken 948 times.
|
948 | PString skippedStr(""); |
91 |
1/1✓ Branch 1 taken 948 times.
|
948 | PString token(parser.getNextToken(skippedStr)); |
92 |
2/2✓ Branch 1 taken 948 times.
✓ Branch 4 taken 948 times.
|
948 | body += skippedStr + token; |
93 | 948 | ++tokenIndex; | |
94 | 948 | } | |
95 | } | ||
96 | 8 | return b; | |
97 | 10 | } | |
98 | |||
99 | |||
100 | ///Process the program | ||
101 | /** @param vecInputFile : vector of input files | ||
102 | * @param command : command to be used | ||
103 | * @param firstToken : Index of the first token to be tested | ||
104 | * @param lastToken : Index of the last token to be tested | ||
105 | * @param incrementToken : Increment of the index to be used | ||
106 | * @param delimitor : All delimitor characters to be used to tokenise the input file | ||
107 | * @param isNoFail : true to ignore if the tested program failed or not, false for normal behaviour | ||
108 | * @return true on success, false otherwise | ||
109 | */ | ||
110 | 10 | bool processVecFile(const PVecPath & vecInputFile, const PString & command, | |
111 | size_t firstToken, size_t lastToken, size_t incrementToken, const PString & delimitor, bool isNoFail) | ||
112 | { | ||
113 | 10 | bool b(true); | |
114 |
5/6✓ Branch 3 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 10 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 10 times.
✓ Branch 8 taken 10 times.
|
20 | for(PVecPath::const_iterator it(vecInputFile.begin()); it != vecInputFile.end() && b; ++it){ |
115 |
1/1✓ Branch 2 taken 10 times.
|
10 | b &= processFile(*it, command, firstToken, lastToken, incrementToken, delimitor, isNoFail); |
116 | } | ||
117 | 10 | return b; | |
118 | } | ||
119 | |||
120 | |||
121 | 10 | int main(int argc, char** argv){ | |
122 |
1/1✓ Branch 1 taken 10 times.
|
10 | OptionParser parser = createOptionParser(); |
123 |
1/1✓ Branch 1 taken 10 times.
|
10 | parser.parseArgument(argc, argv); |
124 | |||
125 |
1/1✓ Branch 1 taken 10 times.
|
10 | const OptionMode & defaultMode = parser.getDefaultMode(); |
126 | 10 | PVecPath vecInputFile; | |
127 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
|
10 | defaultMode.getValue(vecInputFile, "input"); |
128 | |||
129 |
1/1✓ Branch 1 taken 10 times.
|
10 | PString command(""); |
130 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
|
10 | defaultMode.getValue(command, "command"); |
131 | |||
132 | 10 | size_t firstToken(0lu), lastToken(-1lu), incrementToken(1lu); | |
133 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
|
10 | defaultMode.getValue(firstToken, "firsttoken"); |
134 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
|
10 | defaultMode.getValue(lastToken, "lasttoken"); |
135 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
|
10 | defaultMode.getValue(incrementToken, "incrementtoken"); |
136 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if(incrementToken == 0lu){incrementToken = 1lu;} //incrementToken cannot be 0 |
137 | |||
138 |
1/1✓ Branch 1 taken 10 times.
|
10 | PString delimitor(""); |
139 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
|
10 | defaultMode.getValue(delimitor, "delimitor"); |
140 | |||
141 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
|
10 | bool isNoFail = defaultMode.isOptionExist("nofail"); |
142 | |||
143 |
1/1✓ Branch 1 taken 10 times.
|
10 | bool b(processVecFile(vecInputFile, command, |
144 | firstToken, lastToken, incrementToken, delimitor, isNoFail)); | ||
145 | 10 | return b - 1; | |
146 | 10 | } | |
147 | |||
148 |