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

void test_unspecified_length (string s[])
{
    printf ("Verify compile of functions with array parameters of unspecified length:\n");
    printf ("\tlength of s is %d\n", arraylength(s));
}



shader
test (color cparamarray[2] = { color(0,1,2), color(3,4,5) })
{
    int iarray[3] = { 10, 11, 12 };
    // Initialization of farray includes one initializer that looks like
    // an int rather than a float
    float farray[3] = { 10.5, 11.5, 12 };
    // Initialization of carray includes one element that looks like a
    // float rather than a color
    color carray[3] = { color(.1,.2,.3), 0.2, color(.3,.3,.3) };
    matrix marray[3] = { matrix(1), matrix(2), matrix(3) };
    string sarray[3] = { "Able", "Baker", "Charlie" };

    printf ("Test basic array initialization and referencing:\n");
    printf ("iarray = [%d %d %d]\n", iarray[0], iarray[1], iarray[2]);
    printf ("farray = [%g %g %g]\n", farray[0], farray[1], farray[2]);
    printf ("carray = [(%g) (%g) (%g)]\n", carray[0], carray[1], carray[2]);
    printf ("marray = [(%g) (%g) (%g)]\n", marray[0], marray[1], marray[2]);
    printf ("sarray = [\"%s\" \"%s\" \"%s\"]\n", sarray[0], sarray[1], sarray[2]);
    printf ("arraylength(carray) = %d\n", arraylength(carray));
    printf ("parameter cparamarray = [(%g) (%g)]\n",
            cparamarray[0], cparamarray[1]);

    printf ("\nTest array element assignment:\n");
    printf ("After 'farray[1] = 14; farray[2] = 15.5;'...\n");
    farray[1] = 14;
    farray[2] = 15.5;
    printf ("  farray = [%g %g %g]\n", farray[0], farray[1], farray[2]);
    printf ("carray[1][2] = %g\n", carray[1][2]);
    carray[1][2] = 3.1;
    printf ("after 'carray[1][2] = 3.1', carray[1][2] = %g\n", carray[1][2]);
    printf ("carray = [(%g) (%g) (%g)]\n", carray[0], carray[1], carray[2]);
    printf ("marray[2][1][1] = %g, marray[2][1][2] = %g\n",
            marray[2][1][1], marray[2][1][2]);
    printf ("after 'marray[2][1][3] = 100',\n");
    marray[2][1][3] = 100;
    printf ("marray[2] = (%g)\n", marray[2]);

    printf ("\nTest varying array assignment and reference:\n");
    printf ("After carray[2] = color(u,v,4)...\n");
    carray[2] = color (u, v, 4);
    printf ("carray = [(%g) (%g) (%g)]\n", carray[0], carray[1], carray[2]);

    printf ("\nTest whole-array assignment:\n");
    float farray2[3];
    farray2 = farray;
    printf ("farray = [%g %g %g]\n",
            farray[0], farray[1], farray[2]);
    printf ("after farray2 = farray, farray2 = [%g %g %g]\n\n",
            farray2[0], farray2[1], farray2[2]);

    test_unspecified_length (sarray);

    {
        // Test message passing an array
        int imess[3];
        setmessage ("i", iarray);
        getmessage ("i", imess);
        printf ("Test message passing an array: imess = [%d %d %d]\n",
                imess[0], imess[1], imess[2]);
    }

    {
        printf ("\nTest array of closures:\n");
        closure color C[3];
        C[0] = diffuse (N);
        C[1] = 0.5 * reflection (N);
        C[2] = 0.25 * refraction (N, 1.5);
        for (int i = 0;  i < 3;  ++i) {
            printf ("C[%d] = %s\n", i, C[i]);
        }

        // Test passing an array of closures to a function
        void print_closure_array (closure color C[]) {
            for (int i = 0;  i < 3;  ++i) {
                printf ("pca  C[%d] = %s\n", i, C[i]);
            }
        }
        print_closure_array (C);

        // Test message passing an array of closures
        closure color Cmess[3];
        setmessage ("C", C);
        getmessage ("C", Cmess);
        printf ("Testing message passing of array of closures:\n");
        print_closure_array (Cmess);
    }
    printf ("\n\n");
}
