/*
 * 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"

void testbench_gcd(liquid_autotest __q__,
                   unsigned int _gcd,
                   unsigned int _p,
                   unsigned int _q)
{
	unsigned int P = _p * _gcd;
	unsigned int Q = _q * _gcd;
	unsigned int gcd_test = liquid_gcd(P,Q);
    liquid_log_debug("  gcd(%12u,%12u) = %12u (expected %u)", P, Q, gcd_test, _gcd);
    LIQUID_CHECK(gcd_test ==  _gcd);
}

LIQUID_AUTOTEST(gcd_one,"gcd: no common roots (gcd = 1)","",0.1)
{
    testbench_gcd(__q__, 1,  2,  3);
    testbench_gcd(__q__, 1,  2,  5);
    testbench_gcd(__q__, 1,  2,  7);
    testbench_gcd(__q__, 1,  7,  2);
    testbench_gcd(__q__, 1, 17, 19);
    testbench_gcd(__q__, 1, 23, 31);
    testbench_gcd(__q__, 1, 2*2*2*2*2, 3*5*7*19);
}

LIQUID_AUTOTEST(gcd_edge_cases,"gcd: edge cases","",0.1)
{
    testbench_gcd(__q__, 1,         1, 1);
    testbench_gcd(__q__, 1,         1, 2);
    testbench_gcd(__q__, 1,         1, 2400);
    testbench_gcd(__q__, 12345,     1, 1);    // P==Q
    testbench_gcd(__q__, (1<<17)-1, 1, 1);    // P==Q
}

LIQUID_AUTOTEST(gcd_base,"gcd: base tests","",0.1)
{
    testbench_gcd(__q__, 2*2*3*5*7, 2*3, 17);
    testbench_gcd(__q__, 2*2*3*5*7, 2*3, 17*17);
    testbench_gcd(__q__, 2*2*3*5*7, 2*3, 17*17*17);
    testbench_gcd(__q__, 11,        3,   1);
    testbench_gcd(__q__, 3,         127, 131);
    testbench_gcd(__q__, 131,       127, 3);
    testbench_gcd(__q__, 127,       131, 3);
}

