Skip to content
Snippets Groups Projects
Commit 41b6a2b7 authored by Simon Heybrock's avatar Simon Heybrock
Browse files

Re #0. Tests for IndexInfo detector -> spectrum mapper.

parent 1b698aea
No related branches found
No related tags found
No related merge requests found
......@@ -202,14 +202,18 @@ SpectrumIndexSet IndexInfo::makeIndexSet(
std::vector<GlobalSpectrumIndex>
IndexInfo::globalSpectrumIndicesFromDetectorIndices(
const std::vector<size_t> &detectorIndices) const {
std::vector<bool> detectorMap;
if (!m_spectrumDefinitions)
throw std::runtime_error("IndexInfo::"
"globalSpectrumIndicesFromDetectorIndices -- no "
"spectrum definitions available");
std::vector<char> detectorMap;
for (const auto &index : detectorIndices) {
// IndexInfo has no knowledge of the maximum detector index so we workaround
// this knowledge gap by assuming below that any index beyond the end of the
// map is `false`.
// map is 0.
if (index >= detectorMap.size())
detectorMap.resize(index + 1, false);
detectorMap[index] = true;
detectorMap.resize(index + 1, 0);
detectorMap[index] = 1;
}
std::vector<GlobalSpectrumIndex> spectrumIndices;
......@@ -217,13 +221,20 @@ IndexInfo::globalSpectrumIndicesFromDetectorIndices(
const auto &spectrumDefinition = m_spectrumDefinitions->operator[](i);
if (spectrumDefinition.size() == 1) {
const auto detectorIndex = spectrumDefinition[0].first;
if (detectorMap.size() > detectorIndex && detectorMap[detectorIndex])
if (detectorMap.size() > detectorIndex &&
detectorMap[detectorIndex] != 0) {
if (detectorMap[detectorIndex] > 1)
throw std::runtime_error(
"Multiple spectra correspond to the same detector");
// Increment flag to catch two spectra mapping to same detector.
++detectorMap[detectorIndex];
spectrumIndices.push_back(i);
}
}
if (spectrumDefinition.size() > 1)
throw std::runtime_error("SpectrumDefinition containes multiple entries. "
"No unique mapping from detector to spectrum "
"possible.");
"possible");
}
if (detectorIndices.size() != spectrumIndices.size())
throw std::runtime_error(
......
......@@ -187,6 +187,90 @@ public:
TS_ASSERT_EQUALS(info.spectrumDefinitions().get(), defs.get());
}
void test_globalSpectrumIndicesFromDetectorIndices_fails_without_spec_defs() {
IndexInfo info(3);
std::vector<size_t> detectorIndices{6, 8};
TS_ASSERT_THROWS_EQUALS(
info.globalSpectrumIndicesFromDetectorIndices(detectorIndices),
const std::runtime_error &e, std::string(e.what()),
"IndexInfo::globalSpectrumIndicesFromDetectorIndices -- no spectrum "
"definitions available");
}
void test_globalSpectrumIndicesFromDetectorIndices_fails_multiple() {
IndexInfo info(3);
std::vector<size_t> detectorIndices{6, 8};
std::vector<SpectrumDefinition> specDefs(3);
specDefs[0].add(6);
specDefs[1].add(7);
specDefs[1].add(77);
specDefs[2].add(8);
info.setSpectrumDefinitions(specDefs);
TS_ASSERT_THROWS_EQUALS(
info.globalSpectrumIndicesFromDetectorIndices(detectorIndices),
const std::runtime_error &e, std::string(e.what()),
"SpectrumDefinition containes multiple entries. No unique mapping from "
"detector to spectrum possible");
}
void test_globalSpectrumIndicesFromDetectorIndices_fails_missing() {
IndexInfo info(3);
std::vector<size_t> detectorIndices{6, 8};
std::vector<SpectrumDefinition> specDefs(3);
// Nothing maps to 8
specDefs[0].add(6);
specDefs[1].add(7);
info.setSpectrumDefinitions(specDefs);
TS_ASSERT_THROWS_EQUALS(
info.globalSpectrumIndicesFromDetectorIndices(detectorIndices),
const std::runtime_error &e, std::string(e.what()),
"Some of the requested detectors do not have a corresponding spectrum");
}
void test_globalSpectrumIndicesFromDetectorIndices_fails_conflict() {
IndexInfo info(3);
std::vector<size_t> detectorIndices{6, 8};
std::vector<SpectrumDefinition> specDefs(3);
// Two indices map to same detector.
specDefs[0].add(6);
specDefs[1].add(6);
specDefs[2].add(8);
info.setSpectrumDefinitions(specDefs);
TS_ASSERT_THROWS_EQUALS(
info.globalSpectrumIndicesFromDetectorIndices(detectorIndices),
const std::runtime_error &e, std::string(e.what()),
"Multiple spectra correspond to the same detector");
}
void test_globalSpectrumIndicesFromDetectorIndices_fails_conflict_miss() {
IndexInfo info(3);
std::vector<size_t> detectorIndices{6, 8};
std::vector<SpectrumDefinition> specDefs(3);
// Two indices map to same detector, but additionally one is missing.
specDefs[0].add(6);
specDefs[1].add(6);
info.setSpectrumDefinitions(specDefs);
TS_ASSERT_THROWS_EQUALS(
info.globalSpectrumIndicesFromDetectorIndices(detectorIndices),
const std::runtime_error &e, std::string(e.what()),
"Multiple spectra correspond to the same detector");
}
void test_globalSpectrumIndicesFromDetectorIndices() {
IndexInfo info(3);
std::vector<size_t> detectorIndices{6, 8};
std::vector<SpectrumDefinition> specDefs(3);
specDefs[0].add(6);
specDefs[1].add(7);
specDefs[2].add(8);
info.setSpectrumDefinitions(specDefs);
const auto &indices =
info.globalSpectrumIndicesFromDetectorIndices(detectorIndices);
TS_ASSERT_EQUALS(indices.size(), detectorIndices.size());
TS_ASSERT_EQUALS(indices[0], 0);
TS_ASSERT_EQUALS(indices[1], 2);
}
void test_StorageMode_Cloned() {
runParallel(run_StorageMode_Cloned);
// Trivial: Run with one partition.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment