diff --git a/gamgee/shared_field.h b/gamgee/shared_field.h index 2e42e2a3c..57cc1afdb 100644 --- a/gamgee/shared_field.h +++ b/gamgee/shared_field.h @@ -79,6 +79,33 @@ class SharedField { SharedField(SharedField&& other) = default; ///< @brief safely moves the data from one SharedField to a new one without making any copies SharedField& operator=(SharedField&& other) = default; ///< @brief safely moves the data from one SharedField to the other without making any copies + /** + * @brief compares two SharedField objects in the following order: memory address, size and values. + * @param other something to compare to + * @return true if the objects are the same (memory address-wise), or contain exactly the same values. Value comparison is dictated by TYPE's operator== implementation + */ + bool operator==(const SharedField& other) const { + if (this == &other) + return true; + if (size() != other.size()) + return false; + for (auto i=0u; i != size(); ++i) { + if (operator[](i) != other[i]) + return false; + } + return true; + } + + /** + * @brief compares two SharedField objects in the following order: memory address, size and values. + * @param other something to compare to + * @return true if the objects are not the same (memory address-wise), or contain different number of values, or the values are not exactly the same. Value comparison is dictated by TYPE's operator== implementation + */ + bool operator!=(const SharedField& other) const { + return !(*this == other); + } + + /** * @brief random access to a given value for reading or writing * @param index must be between 0 and the number of values for this record diff --git a/test/variant_reader_test.cpp b/test/variant_reader_test.cpp index 57c63981d..599447b76 100644 --- a/test/variant_reader_test.cpp +++ b/test/variant_reader_test.cpp @@ -303,13 +303,15 @@ void check_shared_field_api(const Variant& record, const uint32_t truth_index) { BOOST_CHECK(desc_bool_idx); BOOST_CHECK_THROW(desc_float_idx[0], invalid_argument); BOOST_CHECK_THROW(desc_integer_idx[0], invalid_argument); - // BOOST_CHECK_EQUAL(desc, record.shared_field_as_string("DESC")); // needs operator == on shared fields - // BOOST_CHECK_EQUAL(desc_idx, record.shared_field_as_string(desc_index)); // needs operator == on shared fields + BOOST_CHECK(desc == record.shared_field_as_string("DESC")); + BOOST_CHECK(desc_idx == record.shared_field_as_string(desc_index)); } - // BOOST_CHECK_EQUAL(an, record.shared_field_as_integer("AN")); // needs operator == on shared fields - // BOOST_CHECK_EQUAL(an, record.shared_field_as_integer(header.field_index("AN"))); // needs operator == on shared fields - // BOOST_CHECK_EQUAL(af, record.shared_field_as_float("AF")); // needs operator == on shared fields - // BOOST_CHECK_EQUAL(af, record.shared_field_as_float(header.field_index("AF"))); // needs operator == on shared fields + BOOST_CHECK(an == record.shared_field_as_integer("AN")); + BOOST_CHECK(an == record.shared_field_as_integer(header.field_index("AN"))); + BOOST_CHECK(af == record.shared_field_as_float("AF")); + BOOST_CHECK(af == record.shared_field_as_float(header.field_index("AF"))); + BOOST_CHECK(an != record.shared_field_as_integer("AF")); + BOOST_CHECK(af != record.shared_field_as_float(header.field_index("AN"))); } void check_genotype_api(const Variant& record, const uint32_t truth_index) {