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



shader
test (string filename = "../common/textures/grid.tx",
      int date = 1)
{
    printf ("Executing...\n");
    int r;

    int resolution[2];
    r = gettextureinfo (filename, "resolution", resolution);
    printf ("%s resolution: %d %d (%d)\n", filename, resolution[0], resolution[1], r);

    int channels;
    r = gettextureinfo (filename, "channels", channels);
    printf ("%s channels: %d (%d)\n", filename, channels, r);

    string texturetype;
    r = gettextureinfo (filename, "texturetype", texturetype);
    printf ("%s texturetype: %s (%d)\n", filename, texturetype, r);

    string textureformat;
    r = gettextureinfo (filename, "textureformat", textureformat);
    printf ("%s textureformat: %s (%d)\n", filename, textureformat, r);

    int datawin[4], displaywin[4];
    r = gettextureinfo (filename, "datawindow", datawin);
    printf ("%s datawindow: %d %d %d %d (%d)\n", filename,
            datawin[0], datawin[1], datawin[2], datawin[3], r);
    r = gettextureinfo (filename, "displaywindow", displaywin);
    printf ("%s displaywindow: %d %d %d %d (%d)\n", filename,
            displaywin[0], displaywin[1], displaywin[2], displaywin[3], r);

    // Test arbitrary metadata
    string datetime;
    r = gettextureinfo (filename, "DateTime", datetime);
    if (date)
        printf ("%s datetime: %s (%d)\n", filename, datetime, r);

    // Test average and constant retrieval
    color avgcolor;
    float avgalpha;
    r = gettextureinfo (filename, "averagecolor", avgcolor);
    if (r)
        printf ("%s averagecolor: %g\n", filename, avgcolor);
    else
        printf ("%s unknown averagecolor\n", filename);
    r = gettextureinfo (filename, "averagealpha", avgalpha);
    if (r)
        printf ("%s averagealpha: %g\n", filename, avgalpha);
    else
        printf ("%s unknown averagealpha\n", filename);
    r = gettextureinfo (filename, "constantcolor", avgcolor);
    if (r)
        printf ("%s constantcolor: %g\n", filename, avgcolor);
    else
        printf ("%s unknown constantcolor\n", filename);
    r = gettextureinfo (filename, "constantalpha", avgalpha);
    if (r)
        printf ("%s constantalpha: %g\n", filename, avgalpha);
    else
        printf ("%s unknown constantalpha\n", filename);

    // Test failure of unfound info name
    string foobar = "not found";
    r = gettextureinfo (filename, "foobar", foobar);
    printf ("%s foobar: %s (%d)\n", filename, foobar, r);

    // Test failure of unfound file name
    string data = "not found";
    string badfile = "badfile";
    r = gettextureinfo (badfile, "textureformat", data);
    printf ("%s data: %s (%d)\n", badfile, data, r);

    // Make a query of a bad file inside a conditional -- the idea is that
    // we should NOT see an error message if the statement is not executed.
    if (u > 2) {
        string data = "not found";
        string badfile = "badfile2";
        r = gettextureinfo (badfile, "textureformat", data);
        printf ("%s data: %s (%d)\n", badfile, data, r);
    }

    // Test existence of a valid file
    {
        int e;
        r = gettextureinfo (filename, "exists", e);
        printf ("%s exists? %d (return %d)\n", filename, e, r);
    }

    // Test existence of a nonexistant file
    {
        int e;
        string badfile = "badfile3";
        r = gettextureinfo (badfile, "exists", e);
        printf ("%s exists? %d (return %d)\n", badfile, e, r);
    }


}
