diff --git a/src/spike.hpp b/src/spike.hpp index 8a883f01dbb486dd47d34efb960dd9bbd7a43fef..d45ea97e74e1e412e08417eaa19a020689ca3143 100644 --- a/src/spike.hpp +++ b/src/spike.hpp @@ -20,6 +20,12 @@ struct basic_spike { basic_spike(id_type s, time_type t): source(s), time(t) {} + + /// Less than comparison operator for nest::mc::spike<> values: + /// spikes are ordered by spike time, for use in sorting and queueing. + friend bool operator<(basic_spike lhs, basic_spike rhs) { + return lhs.time < rhs.time; + } }; /// Standard specialization: @@ -33,12 +39,3 @@ template <typename I> std::ostream& operator<<(std::ostream& o, nest::mc::basic_spike<I> s) { return o << "spike[t " << s.time << ", src " << s.source << "]"; } - -/// Less than comparison operator for nest::mc::spike<> values: -/// spikes are ordered by spike time, for use in sorting and queueing. -template <typename I> -bool operator<(nest::mc::basic_spike<I> lhs, nest::mc::basic_spike<I> rhs) { - return lhs.time < rhs.time; -} - - diff --git a/tests/unit/test_spikes.cpp b/tests/unit/test_spikes.cpp index 5e759025138a493ebdc449b3ce7ac9c69c87e248..402305642bfbee6046481a96d20eff529a2f4161 100644 --- a/tests/unit/test_spikes.cpp +++ b/tests/unit/test_spikes.cpp @@ -17,6 +17,39 @@ using backend = multicore::backend; using backend = USE_BACKEND; #endif +TEST(spikes, ordering) { + { + spike s1({0,0}, 1), s2({0,0}, 2); + EXPECT_TRUE(s1<s2); + } + { + spike s1({0,0}, 1), s2({0,0}, 1); + EXPECT_FALSE(s1<s2); + } + { + spike s1({0,0}, 2), s2({0,0}, 1); + EXPECT_FALSE(s1<s2); + } +} + +TEST(spikes, sorting) { + std::vector<spike> spikes = { + {{0,0}, 1}, + {{0,0}, 2}, + {{0,0}, 0}, + {{0,0}, 3}, + {{0,0}, 1}, + {{0,0}, 2}, + }; + + std::sort(spikes.begin(), spikes.end()); + + auto it = spikes.begin(); + while (++it!=spikes.end()) { + EXPECT_TRUE(it->time >= (it-1)->time); + } +} + TEST(spikes, threshold_watcher) { using backend = multicore::backend; using size_type = backend::size_type;