/*
 * Copyright (c) 2007 - 2026 Joseph Gaeddert
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "liquid.autotest.h"
#include "liquid.h"

// validate autocorrelation properties of complementary codes
void testbench_ccodes(liquid_autotest __q__, unsigned int _n)
{
    // create and initialize codes
    bsequence a = bsequence_create(_n);
    bsequence b = bsequence_create(_n);
    bsequence_create_ccodes(a, b);

    // generate test sequences
    bsequence ax = bsequence_create(_n);
    bsequence bx = bsequence_create(_n);
    bsequence_create_ccodes(ax, bx);

    unsigned int i;
    int raa, rbb;
    for (i=0; i<_n; i++) {
        // correlate like sequences
        raa = 2*bsequence_correlate(a,ax) - _n;
        rbb = 2*bsequence_correlate(b,bx) - _n;

        if (i==0) { LIQUID_CHECK(raa+rbb == (int)(2*_n)); }
        else      { LIQUID_CHECK(raa+rbb == 0);           }

        bsequence_circshift(ax);
        bsequence_circshift(bx);
    }

    // clean up memory
    bsequence_destroy(a);
    bsequence_destroy(b);
    bsequence_destroy(ax);
    bsequence_destroy(bx);
}

LIQUID_AUTOTEST(complementary_code_n8,  "","",0.1) { testbench_ccodes(__q__,  8); }
LIQUID_AUTOTEST(complementary_code_n16, "","",0.1) { testbench_ccodes(__q__, 16); }
LIQUID_AUTOTEST(complementary_code_n32, "","",0.1) { testbench_ccodes(__q__, 32); }
LIQUID_AUTOTEST(complementary_code_n64, "","",0.1) { testbench_ccodes(__q__, 64); }
LIQUID_AUTOTEST(complementary_code_n128,"","",0.1) { testbench_ccodes(__q__,128); }
LIQUID_AUTOTEST(complementary_code_n256,"","",0.1) { testbench_ccodes(__q__,256); }
LIQUID_AUTOTEST(complementary_code_n512,"","",0.1) { testbench_ccodes(__q__,512); }

