Skip to content

Commit

Permalink
update unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
KedoKudo committed Sep 10, 2024
1 parent 9bebd6e commit 6c73b1e
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions sophiread/SophireadCLI/tests/test_sophiread_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,23 @@ TEST_F(SophireadCoreTest, TimedSaveTOFImagingToTIFF) {
EXPECT_TRUE(std::filesystem::exists("test_tof/test_bin_0002.tiff"));
EXPECT_TRUE(std::filesystem::exists("test_tof/test_bin_0003.tiff"));
EXPECT_TRUE(std::filesystem::exists("test_tof/test_Spectra.txt"));

// Check spectra file contents
std::ifstream spectra_file("test_tof/test_Spectra.txt");
std::string line;
std::vector<std::string> lines;
while (std::getline(spectra_file, line)) {
lines.push_back(line);
}
// close the file
spectra_file.close();

EXPECT_EQ(lines.size(), 4); // Header + 3 data lines
EXPECT_EQ(lines[0], "shutter_time,counts");
EXPECT_EQ(lines[1], "0.1,100"); // 10 * 10 * 1 = 100 counts for each bin
EXPECT_EQ(lines[2], "0.2,100");
EXPECT_EQ(lines[3], "0.3,100");

std::filesystem::remove_all("test_tof");
}

Expand Down Expand Up @@ -180,6 +197,46 @@ TEST_F(SophireadCoreTest, UpdateTOFImages) {
EXPECT_TRUE(updated);
}

TEST_F(SophireadCoreTest, CalculateSpectralCounts) {
std::vector<std::vector<std::vector<unsigned int>>> tof_images(
3, std::vector<std::vector<unsigned int>>(
5, std::vector<unsigned int>(
5, 2))); // 3 bins, 5x5 images, all values 2

auto spectral_counts = sophiread::calculateSpectralCounts(tof_images);

EXPECT_EQ(spectral_counts.size(), 3); // 3 bins
for (const auto& count : spectral_counts) {
EXPECT_EQ(count, 50); // 5 * 5 * 2 = 50 for each bin
}
}

TEST_F(SophireadCoreTest, WriteSpectralFile) {
std::vector<uint64_t> spectral_counts = {10, 20, 30};
std::vector<double> tof_bin_edges = {0.0, 0.1, 0.2, 0.3};
std::string filename = "test_spectra.txt";

sophiread::writeSpectralFile(filename, spectral_counts, tof_bin_edges);

EXPECT_TRUE(std::filesystem::exists(filename));

// Read and check file contents
std::ifstream file(filename);
std::string line;
std::vector<std::string> lines;
while (std::getline(file, line)) {
lines.push_back(line);
}

EXPECT_EQ(lines.size(), 4); // Header + 3 data lines
EXPECT_EQ(lines[0], "shutter_time,counts");
EXPECT_EQ(lines[1], "0.1,10");
EXPECT_EQ(lines[2], "0.2,20");
EXPECT_EQ(lines[3], "0.3,30");

std::filesystem::remove(filename);
}

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
Expand Down

0 comments on commit 6c73b1e

Please sign in to comment.