(
    file_name_base:sconcat("test_",random(10^10)),
    file_name_svg:sconcat(maxima_tempdir,"/",file_name_base),
    file_name_gnuplot:sconcat(file_name_base,".gnuplot"),
    file_name_data:sconcat(maxima_tempdir,"/",file_name_base,".data"),
    file_name_data_gnuplot:sconcat(file_name_base,".data"),
    /* Define a function that tests if the gnuplot file created by draw contains
       a set of lines.
       */
    gnuplot_contains([x]):=block(
        [
            infile:openr(sconcat(maxima_tempdir,"/",file_name_gnuplot)),
            line,
	    retval,
            lines:[]
        ],
        while(stringp(line:readline(infile))) do
            push(line,lines),
        retval:emptyp(
            setdifference(
                setify(x),
                setify(lines)
            )
        ),
	close(infile),
	return(retval)
    ),
    /* Define a function that tests if the gnuplot file created by draw contains
       a string.
       */
    gnuplot_contains_string(x):=block(
        [
            infile:openr(sconcat(maxima_tempdir,"/",file_name_gnuplot)),
            line,retval:false
        ],
        while(stringp(line:readline(infile))) do
            if numberp(ssearch(x,line)) then retval:true,
	close(infile),
	return(retval)
    ),
    0
);
0$

/* Test if "title=" affects the gnuplot file. */
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    explicit(sin(x),x,1,10)
),0);
0$
gnuplot_contains("set title ''");
true$

(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    title="Test",
    explicit(sin(x),x,1,10)
),0);
0$
gnuplot_contains("set title 'Test'");
true$

/* Test "contour=". */
(draw3d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    contour='none,
    explicit(sin(x*y),x,1,10,y,1,10)
),0);
0$
(draw3d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    contour='base,
    explicit(sin(x*y),x,1,10,y,1,10)
),0);
0$
gnuplot_contains("set contour base");
true$
(draw3d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    contour='both,
    explicit(sin(x*y),x,1,10,y,1,10)
),0);
0$
gnuplot_contains("set contour both");
true$
(draw3d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    contour='surface,
    explicit(sin(x*y),x,1,10,y,1,10)
),0);
0$
gnuplot_contains("set contour surface");
true$
(draw3d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    contour='map,
    explicit(sin(x*y),x,1,10,y,1,10)
),0);
0$
gnuplot_contains("set contour base","unset surface");
true$

/* Test "axis_3d=". */
(draw3d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    axis_3d=false,
    explicit(sin(x*y),x,1,10,y,1,10)
),0);
0$
gnuplot_contains("unset xtics", "unset ytics", "unset ztics", "set border 0");
true$
(draw3d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    axis_3d=true,
    explicit(sin(x*y),x,1,10,y,1,10)
),0);
0$
gnuplot_contains("unset xtics");
false$
gnuplot_contains("unset ytics");
false$
gnuplot_contains("unset ztics");
false$
gnuplot_contains("set border 0");
false$

/* test the other axis_* commands */
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    axis_bottom = false,
    explicit(sin(x),x,1,10)
),0);
0$
gnuplot_contains("set border 14");
true$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    axis_top = false,
    explicit(sin(x),x,1,10)
),0);
0$
gnuplot_contains("set border 11");
true$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    axis_left = false,
    explicit(sin(x),x,1,10)
),0);
0$
gnuplot_contains("set border 13");
true$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    axis_right = false,
    explicit(sin(x),x,1,10)
),0);
0$
gnuplot_contains("set border 7");
true$


/* test all combination of border and transparent */
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    border = true, transparent = true,
    polygon([[3,2],[7,2],[5,5]])
),0);
0$
gnuplot_contains_string("lt ");
true$
gnuplot_contains_string("filledcurves ");
false$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    border = false, transparent = false,
    polygon([[3,2],[7,2],[5,5]])
),0);
0$
gnuplot_contains_string("lt ");
false$
gnuplot_contains_string("filledcurves ");
true$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    border = true, transparent = false,
    polygon([[3,2],[7,2],[5,5]])
),0);
0$
gnuplot_contains_string("lt ");
true$
gnuplot_contains_string("filledcurves ");
true$

/* test background_color */
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    line_width=50,
    background_color="#FF0000",
    explicit(sin(x),x,0,10)
),0);
0$
gnuplot_contains_string("obj 1 fc rgb '#ff0000'");
true$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    line_width=50,
    background_color="#00FF0080",
    explicit(sin(x),x,0,10)
),0);
0$
gnuplot_contains_string("obj 1 fc rgb '#00ff0080'");
true$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    line_width=50,
    background_color='yellow,
    explicit(sin(x),x,0,10)
),0);
0$
gnuplot_contains_string("obj 1 fc rgb '#FFFF00'");
true$

/* test line_width and line colors including semi-transparent ones */
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    line_width=50,
    color="#FF0000",
    explicit(sin(x),x,0,10),
    color="#0000FF80",
    explicit(cos(x),x,0,10)
),0);
0$
gnuplot_contains_string("lw 50");
true$
gnuplot_contains_string("lw 50");
true$
gnuplot_contains_string("lc rgb '#800000ff'");
true$
gnuplot_contains_string("lc rgb '#ff0000'");
true$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    line_width=50,
    color='red,
    explicit(sin(x),x,0,10),
    color='blue,
    explicit(cos(x),x,0,10)
),0);
0$
gnuplot_contains_string("lc rgb '#FF0000'");
true$
gnuplot_contains_string("lc rgb '#0000FF'");
true$

/* test colorbox */
(draw(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    gr2d(image(apply('matrix,
        makelist(makelist(random(200),i,1,30),i,1,30)),0,0,30,30))
),0);
0$
gnuplot_contains("set colorbox");
true$
gnuplot_contains("unset colorbox");
false$
(draw(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    gr2d(colorbox = false, image(apply('matrix,
        makelist(makelist(random(200),i,1,30),i,1,30)),0,0,30,30))
),0);
0$
gnuplot_contains("set colorbox");
false$
gnuplot_contains("unset colorbox");
true$

/* test "columns=" */
(draw(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    gr2d(nticks=30,
        parametric(2*cos(t),5*sin(t),t,0,2*%pi)),
    gr2d(polygon([4,5,7],[6,4,2])),
    columns=2),
    0
)$
0$
gnuplot_contains("set size 1.0, 0.5");
false$
gnuplot_contains("set size 0.5, 1.0");
true$
(draw(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    gr2d(title="Ellipse",
        nticks=30,
        parametric(2*cos(t),5*sin(t),t,0,2*%pi)),
    gr2d(title="Triangle",
        polygon([4,5,7],[6,4,2])),
    columns=1),
    0
)$
0$
gnuplot_contains("set size 1.0, 0.5");
true$
gnuplot_contains("set size 0.5, 1.0");
false$
gnuplot_contains("set title 'Ellipse'");
true$
gnuplot_contains("set title 'Triangle'");
true$

/* test "allocation=" */
(draw(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    dimensions=[1123,1124],
    gr2d(
        explicit(x^2,x,-1,1)),
    gr2d(
        allocation = [[1/4, 1/4],[1/2, 1/2]],
        explicit(x^3,x,-1,1),
        grid = true)
    ),
    0
);
0$
gnuplot_contains("set size 0.5, 0.5");
true$
gnuplot_contains("set origin 0.25, 0.25");
true$
gnuplot_contains_string("size 1123, 1124");
true$

/* test "delay=" */
(draw(
    terminal='animated_gif,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    delay     = 100,
    gr2d(explicit(x^2,x,-1,1)),
    gr2d(explicit(x^3,x,-1,1)),
    gr2d(explicit(x^4,x,-1,1))),0);
0$
gnuplot_contains_string("delay 100");
true$
gnuplot_contains_string("set terminal gif enhanced animate");
true$

/* test "enhanced3d=". In this instance we don't look at the .gnuplot file
   but at the data this file causes gnuplot to plot instead.
*/
(draw3d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    enhanced3d='none,data_file_name=file_name_data_gnuplot,
    points([1,2,3,4],[3,5,3,1],[1,7,5,3])),
0);
0$
read_nested_list(file_name_data);
[[1.0,3.0,1.0],[2.0,5.0,7.0],[3.0,3.0,5.0],[4.0,1.0,3.0],[],[]]$
(draw3d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    enhanced3d=false,data_file_name=file_name_data_gnuplot,
    points([1,2,3,4],[3,5,3,1],[1,7,5,3])),
0);
0$
read_nested_list(file_name_data);
[[1.0,3.0,1.0],[2.0,5.0,7.0],[3.0,3.0,5.0],[4.0,1.0,3.0],[],[]]$
(draw3d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    enhanced3d = [x-z/10,x,y,z],data_file_name=file_name_data_gnuplot,
    points([1,2,3,4],[3,5,3,1],[1,7,5,3])),
0);
0$
read_nested_list(file_name_data);
[[1.0,3.0,1.0,0.9],[2.0,5.0,7.0,1.3],[3.0,3.0,5.0,2.5],[4.0,1.0,3.0,3.7],[],[]]$
(draw3d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    enhanced3d=true,data_file_name=file_name_data_gnuplot,
    points([1,2,3,4],[3,5,3,1],[1,7,5,3])),
0);
0$
read_nested_list(file_name_data);
[[1.0,3.0,1.0,1.0],[2.0,5.0,7.0,7.0],[3.0,3.0,5.0,5.0],[4.0,1.0,3.0,3.0],[],[]]$
(draw3d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    enhanced3d = [sin(r*s),r,s],data_file_name=file_name_data_gnuplot,
    explicit(sin(x)*sin(y),x,-2,2,y,-2,2)),
0);
0$

/* For the errors object we can at least test if using any of its variants
   causes an error. */
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    error_type = 'y,
    errors([[1,2,1], [3,5,3], [10,3,1], [17,6,2]])),0);
0$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    error_type = 'x,
    errors([[1,2,1], [3,5,3], [10,3,1], [17,6,2]])),0);
0$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    error_type = 'y,
    errors([[1,2,1,1], [3,5,3,2], [10,3,1,1], [17,6,2,2]])),0);
0$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    error_type = 'x,
    errors([[1,2,1,1], [3,5,3,2], [10,3,1,1], [17,6,2,2]])),0);
0$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    error_type = 'xy,
    errors([[1,2,1,1], [3,5,3,3], [10,3,1,2], [17,6,2,1]])),0);
0$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    error_type = 'boxes,
    errors([[1,2,1,1], [3,5,3,3], [10,3,1,2], [17,6,2,1]])),0);
0$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    error_type = 'xy,
    errors([[1,2,1,1,1,1], [3,5,3,3,1,1], [10,3,1,2,2,2], [17,6,2,1,2,1]])),0);
0$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    error_type = 'boxes,
    errors([[1,2,1,1,2,2], [3,5,3,3,1,1], [10,3,1,2,2,2], [17,6,2,1,2,1]])),0);
0$

/* fill_color and filled_func */
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    line_width=50,
    fill_color="#FF0000",
    filled_func=x,
    explicit(sin(x),x,0,10),
    fill_color="#0000FF80",
    explicit(cos(x),x,0,10)
),0);
0$
gnuplot_contains_string("lc rgb '#800000ff'");
true$
gnuplot_contains_string("lc rgb '#ff0000'");
true$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    line_width=50,
    fill_color='red,
    filled_func=1,
    explicit(sin(x),x,0,10),
    fill_color='green,
    explicit(cos(x),x,0,10)
),0);
0$
gnuplot_contains_string("lc rgb '#FF0000'");
true$
gnuplot_contains_string("lc rgb '#00FF00'");
true$

/* Test if drawing a grid works */
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    explicit(sin(x),x,0,10),
    grid=true
),0);
0$
gnuplot_contains("set grid xtics ytics mxtics mytics");
true$
gnuplot_contains("set mxtics 1");
true$
gnuplot_contains("set mytics 1");
true$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    explicit(sin(x),x,0,10),
    grid=[10,1]
),0);
0$
gnuplot_contains("set grid xtics ytics mxtics mytics");
true$
gnuplot_contains("set mxtics 10");
true$
gnuplot_contains("set mytics 1");
true$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    explicit(sin(x),x,0,10),
    grid=[1,10]
),0);
0$
gnuplot_contains("set grid xtics ytics mxtics mytics");
true$
gnuplot_contains("set mxtics 1");
true$
gnuplot_contains("set mytics 10");
true$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    explicit(sin(x),x,0,10),
    grid=[1.2,10.6]
),0);
0$
gnuplot_contains("set grid xtics ytics mxtics mytics");
true$
gnuplot_contains("set mxtics 1");
true$
gnuplot_contains("set mytics 11");
true$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    explicit(sin(x),x,0,10),
    grid=[1.6,10.4]
),0);
0$
gnuplot_contains("set grid xtics ytics mxtics mytics");
true$
gnuplot_contains("set mxtics 2");
true$
gnuplot_contains("set mytics 10");
true$
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    explicit(sin(x),x,0,10),
    grid=false
),0);
0$
gnuplot_contains("unset grid");
true$

/* Tests for head_length, head_angle, head_both and head_type */
(draw2d(
    terminal='svg,file_name=file_name_svg,gnuplot_file_name=file_name_gnuplot,
    xrange      = [0,10],
    yrange      = [0,9],
    head_length = 0.7,
    head_angle  = 10,
    head_both   = false,
    vector([1,1],[0,6]),
    head_angle  = 20,
    vector([2,1],[0,6]),
    head_angle  = 30,
    vector([3,1],[0,6]),
    head_angle  = 40,
    vector([4,1],[0,6]),
    head_angle  = 60,
    vector([5,1],[0,6]),
    head_angle  = 90,
    head_both   = true,
    vector([6,1],[0,6]),
    head_angle  = 120,
    head_type='filled,
    vector([7,1],[0,6]),
    head_angle  = 160,
    head_type='empty,
    vector([8,1],[0,6]),
    head_angle  = 180,
    head_length = 0.3,
    head_type='nofilled,
    vector([9,1],[0,6]) ),0);
0$
gnuplot_contains_string("vect head size 0.7, 10.0");
true$
gnuplot_contains_string("vect head size 0.7, 20.0");
true$
gnuplot_contains_string("vect head size 0.7, 30.0");
true$
gnuplot_contains_string("vect head size 0.7, 40.0");
true$
gnuplot_contains_string("vect head size 0.7, 60.0");
true$
gnuplot_contains_string("vect heads size 0.7, 90.0");
true$
gnuplot_contains_string("vect heads size 0.7, 120.0 filled");
true$
gnuplot_contains_string("vect heads size 0.7, 160.0 empty");
true$
gnuplot_contains_string("vect heads size 0.3, 180.0 nofilled");
true$

/* Clean up.*/
(load("operatingsystem"),0);
0$
(delete_file(sconcat(file_name_svg,".svg")),0);
0$
(delete_file(sconcat(file_name_svg,".gif")),0);
0$
kill(file_name_base, file_name_data, file_name_data_gnuplot, file_name_svg, file_name_gnuplot, file_name_svg, gnuplot_contains, gnuplot_contains_string);
done$
