nf() test (1)

Test 1

See tests 6 to 8 for the values you'd think would be returned. Processing has the same behaviour.

Tests 6 to 8

The values you'd think test 1 would return, but doesn't.


Test written by Matthew Lam



boolean errors=false;

int xcoord=5;
int xoff=10;
int ycoord=35;
int yinc=15;


void setup() {
  
    PFont font;
		//make sure this font in data dir
    font = loadFont("Eureka-90.vlw"); 
    textFont(font, 12);
    size(500,400);
    background(50);
    noLoop();
} 


void draw() {
        String[] res1=new String[3];
        res1[0]="-01";
        res1[1]="20";
        res1[2]="300";
        int[] inp1=new int[3];
        inp1[0]=-1;
        inp1[1]=20;
        inp1[2]=300;
        outputArr(1,"nf({-1,20,300}, 3)",nf(inp1, 2),res1);
 
        String[] res2=new String[3];
        res2[0]="-001.00";
        res2[1]="020.02";
        res2[2]="300.01";
        float[] inp2=new float[3];
        inp2[0]=-1.002;
        inp2[1]=20.02;
        inp2[2]=300.0123;
        outputArr(2,"nf({-1.002,20.02,300.0123}, 3, 2)",nf(inp2, 3, 2),res2);
        
        String[] res3=new String[3];
        res3[0]="-1";
        res3[1]="20";
        res3[2]="300";
        int[] inp3=new int[3];
        inp3[0]=-1;
        inp3[1]=20;
        inp3[2]=300;
        outputArr(3,"nf({-1,20,300}, -1)",nf(inp3, -1),res3);
        
        String[] res4=new String[3];
        res4[0]="-001";
        res4[1]="000";
        res4[2]="000";
        float[] inp4=new float[3];
        inp4[0]=-1.002;
        inp4[1]=0.49;
        inp4[2]=0.5;
        outputArr(4,"nf({-1.002,0.49,0.5}, 3, -1)",nf(inp4, 3, -1),res4);
        
        String[] res5=new String[3];
        res5[0]="-001";
        res5[1]="001";
        res5[2]="002";
        float[] inp5=new float[3];
        inp5[0]=-1.002;
        inp5[1]=1.49;
        inp5[2]=1.5;
        outputArr(5,"nf({-1.002,1.49,1.5}, 3, -1)",nf(inp5, 3, -1),res5);
 
	xcoord=300;
	ycoord=35;
 
        output(6,"nf(-1, 3)",nf(-1, 3),"-001");
        output(7,"nf(20, 3)",nf(20, 3),"020");
        output(8,"nf(300, 3)",nf(300, 3),"300");
 
	String errMsg="";
	if (errors) {
		fill(255,0,0);
		errMsg="RED tests FAILED.";
	} else {
		fill(0,255,0);
		errMsg="All tests passed.";
	}
	text(errMsg,5,20);
}
 
void output(int ind,String txt,String results,String expected) {
	fill(255,255,255);
	text("["+ind+"] "+txt,xcoord,ycoord+=yinc);
	if (results.equals(expected)) {
		fill(0,255,0);
	} else {
		fill(255,0,0);
		errors=true;
	}
	text(results,xcoord+xoff,ycoord+=yinc);
	ycoord+=yinc;
}
 
void outputArr(int ind,String txt,String[] results,String[] expected) {
	fill(255,255,255);
	text("["+ind+"] "+txt,xcoord,ycoord+=yinc);
 
        String resOut="";
        fill(0,255,0);
        for (int i=0;i=expected.length || !results[i].equals(expected[i])) {
               fill(255,0,0);
               errors=true;
           }
           resOut=resOut+results[i]+", ";
        }
 
	text(resOut,xcoord+xoff,ycoord+=yinc);
	ycoord+=yinc;
}