@@ -150,6 +150,23 @@ TEST_F(SophireadCoreTest, TimedSaveTOFImagingToTIFF) {
150
150
EXPECT_TRUE (std::filesystem::exists (" test_tof/test_bin_0002.tiff" ));
151
151
EXPECT_TRUE (std::filesystem::exists (" test_tof/test_bin_0003.tiff" ));
152
152
EXPECT_TRUE (std::filesystem::exists (" test_tof/test_Spectra.txt" ));
153
+
154
+ // Check spectra file contents
155
+ std::ifstream spectra_file (" test_tof/test_Spectra.txt" );
156
+ std::string line;
157
+ std::vector<std::string> lines;
158
+ while (std::getline (spectra_file, line)) {
159
+ lines.push_back (line);
160
+ }
161
+ // close the file
162
+ spectra_file.close ();
163
+
164
+ EXPECT_EQ (lines.size (), 4 ); // Header + 3 data lines
165
+ EXPECT_EQ (lines[0 ], " shutter_time,counts" );
166
+ EXPECT_EQ (lines[1 ], " 0.1,100" ); // 10 * 10 * 1 = 100 counts for each bin
167
+ EXPECT_EQ (lines[2 ], " 0.2,100" );
168
+ EXPECT_EQ (lines[3 ], " 0.3,100" );
169
+
153
170
std::filesystem::remove_all (" test_tof" );
154
171
}
155
172
@@ -180,6 +197,46 @@ TEST_F(SophireadCoreTest, UpdateTOFImages) {
180
197
EXPECT_TRUE (updated);
181
198
}
182
199
200
+ TEST_F (SophireadCoreTest, CalculateSpectralCounts) {
201
+ std::vector<std::vector<std::vector<unsigned int >>> tof_images (
202
+ 3 , std::vector<std::vector<unsigned int >>(
203
+ 5 , std::vector<unsigned int >(
204
+ 5 , 2 ))); // 3 bins, 5x5 images, all values 2
205
+
206
+ auto spectral_counts = sophiread::calculateSpectralCounts (tof_images);
207
+
208
+ EXPECT_EQ (spectral_counts.size (), 3 ); // 3 bins
209
+ for (const auto & count : spectral_counts) {
210
+ EXPECT_EQ (count, 50 ); // 5 * 5 * 2 = 50 for each bin
211
+ }
212
+ }
213
+
214
+ TEST_F (SophireadCoreTest, WriteSpectralFile) {
215
+ std::vector<uint64_t > spectral_counts = {10 , 20 , 30 };
216
+ std::vector<double > tof_bin_edges = {0.0 , 0.1 , 0.2 , 0.3 };
217
+ std::string filename = " test_spectra.txt" ;
218
+
219
+ sophiread::writeSpectralFile (filename, spectral_counts, tof_bin_edges);
220
+
221
+ EXPECT_TRUE (std::filesystem::exists (filename));
222
+
223
+ // Read and check file contents
224
+ std::ifstream file (filename);
225
+ std::string line;
226
+ std::vector<std::string> lines;
227
+ while (std::getline (file, line)) {
228
+ lines.push_back (line);
229
+ }
230
+
231
+ EXPECT_EQ (lines.size (), 4 ); // Header + 3 data lines
232
+ EXPECT_EQ (lines[0 ], " shutter_time,counts" );
233
+ EXPECT_EQ (lines[1 ], " 0.1,10" );
234
+ EXPECT_EQ (lines[2 ], " 0.2,20" );
235
+ EXPECT_EQ (lines[3 ], " 0.3,30" );
236
+
237
+ std::filesystem::remove (filename);
238
+ }
239
+
183
240
int main (int argc, char ** argv) {
184
241
::testing::InitGoogleTest (&argc, argv);
185
242
return RUN_ALL_TESTS ();
0 commit comments