// Copyright Contributors to the Open Shading Language project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/AcademySoftwareFoundation/OpenShadingLanguage

#ifndef M_E
#define M_E 2.718281828459
#endif


shader
test ()
{
    {
        int i = 137;
        printf ("  abs (%d) = %d\n", i, abs(i));
        printf ("  fabs (%d) = %d\n", i, fabs(i));
        i = -137;
        printf ("  abs (%d) = %d\n", i, abs(i));
        printf ("  fabs (%d) = %d\n", i, fabs(i));
        float a = 101.29;
        float b = 1.5*a;
        printf ("  fabs (%g) = %g\n", a, fabs(a));
        printf ("  floor (%g) = %g\n", a, floor(a));
        printf ("  ceil (%g) = %g\n", a, ceil(a));
        printf ("  trunc (%g) = %g\n", a, trunc(a));
        printf ("  round (%g) = %g\n", a, round(a));
        printf ("  sign (%g) = %g\n", a, sign(a));
        printf ("  sqrt (%g) = %g\n", a, sqrt(a));
        printf ("  inversesqrt (%g) = %g\n", a, inversesqrt(a));
        printf ("  cbrt (%g) = %g\n", a, cbrt(a));
        printf ("  fmod (%g, %g) = %g\n", a, b, fmod(a, b));
        a = -137.59;
        b = 1.5*a;
        printf ("  fabs (%g) = %g\n", a, fabs(a));
        printf ("  floor (%g) = %g\n", a, floor(a));
        printf ("  ceil (%g) = %g\n", a, ceil(a));
        printf ("  trunc (%g) = %g\n", a, trunc(a));
        printf ("  round (%g) = %g\n", a, round(a));
        printf ("  sign (%g) = %g\n", a, sign(a));
        printf ("  sqrt (%g) = %g\n", a, sqrt(a));
        printf ("  inversesqrt (%g) = %g\n", a, inversesqrt(a));
        printf ("  cbrt (%g) = %g\n", a, cbrt(a));

        i = 16;
        int j = 7;
        printf ("  mod (%d, %d) = %d\n", i, j, mod(i, j));
        float fi = 16.0;
        float fj = 7.0;
        printf ("  fmod (%g, %g) = %g\n", fi, fj, fmod(fi, fj));
        printf ("  mod (%g, %g) = %g\n", fi, fj, mod(fi, fj));
        fi = -16.0;
        printf ("  fmod (%g, %g) = %g\n", fi, fj, fmod(fi, fj));
        printf ("  mod (%g, %g) = %g\n", fi, fj, mod(fi, fj));
    }

    { 
        vector a = 101.29;
        vector b = 1.5*a;
        printf ("  fabs (%g) = %g\n", a, fabs(a));
        printf ("  floor (%g) = %g\n", a, floor(a));
        printf ("  ceil (%g) = %g\n", a, ceil(a));
        printf ("  trunc (%g) = %g\n", a, trunc(a));
        printf ("  round (%g) = %g\n", a, round(a));
        printf ("  sign (%g) = %g\n", a, sign(a));
        printf ("  sqrt (%g) = %g\n", a, sqrt(a));
        printf ("  inversesqrt (%g) = %g\n", a, inversesqrt(a));
        printf ("  cbrt (%g) = %g\n", a, cbrt(a));
        printf ("  fmod (%g, %g) = %g\n", a, b, fmod(a, b));
        a = -137.59;
        b = 1.5*a;
        printf ("  fabs (%g) = %g\n", a, fabs(a));
        printf ("  floor (%g) = %g\n", a, floor(a));
        printf ("  ceil (%g) = %g\n", a, ceil(a));
        printf ("  trunc (%g) = %g\n", a, trunc(a));
        printf ("  round (%g) = %g\n", a, round(a));
        printf ("  sign (%g) = %g\n", a, sign(a));
        printf ("  sqrt (%g) = %g\n", a, sqrt(a));
        printf ("  inversesqrt (%g) = %g\n", a, inversesqrt(a));
        printf ("  cbrt (%g) = %g\n", a, cbrt(a));
        printf ("  fmod (%g, %g) = %g\n", a, b, fmod(a, b));
        printf ("  mod (%g, %g) = %g\n", a, b, mod(a, b));
    }

    printf (" varying:\n");
    {
        float a = u;
        float b = 1.5*u;
        printf ("  fabs (%g) = %g\n", a, fabs(a));
        printf ("  floor (%g) = %g\n", a, floor(a));
        printf ("  ceil (%g) = %g\n", a, ceil(a));
        printf ("  trunc (%g) = %g\n", a, trunc(a));
        printf ("  round (%g) = %g\n", a, round(a));
        printf ("  sign (%g) = %g\n", a, sign(a));
        printf ("  sqrt (%g) = %g\n", a, sqrt(a));
        printf ("  inversesqrt (%g) = %g\n", a, inversesqrt(a));
        printf ("  cbrt (%g) = %g\n", a, cbrt(a));
        printf ("  fmod (%g, %g) = %g\n", a, b, fmod(a, b));
        printf ("  mod (%g, %g) = %g\n", a, b, mod(a, b));
    }

    {
        color a = u;
        color b = 1.5*u;
        printf ("  fabs (%g) = %g\n", a, fabs(a));
        printf ("  floor (%g) = %g\n", a, floor(a));
        printf ("  ceil (%g) = %g\n", a, ceil(a));
        printf ("  trunc (%g) = %g\n", a, trunc(a));
        printf ("  round (%g) = %g\n", a, round(a));
        printf ("  sign (%g) = %g\n", a, sign(a));
        printf ("  sqrt (%g) = %g\n", a, sqrt(a));
        printf ("  inversesqrt (%g) = %g\n", a, inversesqrt(a));
        printf ("  cbrt (%g) = %g\n", a, cbrt(a));
        printf ("  fmod (%g, %g) = %g\n", a, b, fmod(a, b));
        printf ("  mod (%g, %g) = %g\n", a, b, mod(a, b));
    }

    {
        // Special regression test: fmod with derivs
        vector vec = u;
        float f = 0.75;
        vector r = fmod(vec, f);
        printf ("  fmod (%g, %g) = %g, derivs %g,  %g\n", vec, f, r, Dx(r), Dy(r));
    }
}
