/* -*- mode: C++ -*- */
//
// LDASTools frameCPP - A library implementing the LIGO/Virgo frame
// specification
//
// Copyright (C) 2020 California Institute of Technology
//
// LDASTools frameCPP is free software; you may redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 (GPLv2) of the
// License or at your discretion, any later version.
//
// LDASTools frameCPP is distributed in the hope that it will be useful, but
// without any warranty or even the implied warranty of merchantability
// or fitness for a particular purpose. See the GNU General Public
// License (GPLv2) for more details.
//
// Neither the names of the California Institute of Technology (Caltech),
// The Massachusetts Institute of Technology (M.I.T), The Laser
// Interferometer Gravitational-Wave Observatory (LIGO), nor the names
// of its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// You should have received a copy of the licensing terms for this
// software included in the file LICENSE located in the top-level
// directory of this package. If you did not, you can view a copy at
// http://dcc.ligo.org/M1500244/LICENSE
//

#ifndef FRAMECPP__OO_INTERFACE__FR_VECT_TEST_ICC
#define FRAMECPP__OO_INTERFACE__FR_VECT_TEST_ICC

#include "framecpp/FrVect.hh"
#include "framecpp/FrHistory.hh"

#include "framecpp/Common/FrameSpec.hh"

// FrVect equality must be content-based: two FrVect objects with the
// same data are equal, two with different data are not, and cross-type
// equality through the type-erased operator==(Object&) is always false.
//
// Prior to the fix in this MR, the typed operator== was a stub that
// returned true unconditionally — which silently passed every test
// that relied on FrVect equality (including the round-trip
// serialization tests below). The asserts here exercise the real
// content comparison through FrVectImpl::Data::operator==.
TEST( FrVectEquality, ContentBasedAndCrossType )
{
    using FrameCPP::FrHistory;
    using FrameCPP::Universal::FrVect;
    using FrameCPP::Common::FrameSpec;

    FrVect::dimension_type dim( 4 );
    const int              data_a[ 4 ] = { 1, 2, 3, 4 };
    const int              data_b[ 4 ] = { 1, 2, 3, 5 };

    const FrVect vect_a{ std::string( "vect" ), 1, &dim, data_a, "unit" };
    const FrVect vect_a_copy{ std::string( "vect" ), 1, &dim, data_a, "unit" };
    const FrVect vect_b{ std::string( "vect" ), 1, &dim, data_b, "unit" };
    const FrVect vect_named_different{
        std::string( "different" ), 1, &dim, data_a, "unit"
    };

    // Same content → equal.
    EXPECT_TRUE( vect_a == vect_a );
    EXPECT_TRUE( vect_a == vect_a_copy );

    // Differing data bytes → unequal.
    EXPECT_FALSE( vect_a == vect_b );

    // Differing name → unequal.
    EXPECT_FALSE( vect_a == vect_named_different );

    // Cross-type through Object& → false (dynamic_cast guard).
    const FrHistory             hist{ "name", 0, "comment" };
    const FrameSpec::Object& vect_as_obj = vect_a;
    const FrameSpec::Object& hist_as_obj = hist;
    EXPECT_FALSE( vect_a == hist_as_obj );
    EXPECT_FALSE( hist == vect_as_obj );

    // Self-type through Object& → true.
    const FrameSpec::Object& vect_a_copy_as_obj = vect_a_copy;
    EXPECT_TRUE( vect_a == vect_a_copy_as_obj );
}

TEST( ArchiverText, FrVectOO )
{
    using FrameCPP::Universal::FrVect;
    const FrVect source_obj;
    auto         source = test_source< FrVect >( source_obj );
    auto         status = test_using_text_archiver( source );
    EXPECT_EQ( 1, status );
}

TEST( ArchiverXML, FrVectOO )
{
    using FrameCPP::Universal::FrVect;
    const FrVect source_obj;
    auto         source = test_source< FrVect >( source_obj );
    auto         status = test_using_text_archiver( source );
    EXPECT_EQ( 1, status );
}

#endif /* FRAMECPP__OO_INTERFACE__FR_VECT_TEST_ICC */
