Source

cowbel / test / file-test.cow

Full commit
/* cowbel test suite
 *
 * Written in 2012 by David Given.
 *
 * To the extent possible under law, the author has dedicated all copyright
 * and related and neighboring rights to this software to the public domain
 * worldwide. This software is distributed without any warranty.
 *
 * Please see the file COPYING.CC0 in the distribution package for more
 * information.
 */

#include "SimpleIO.ch"
#include "File.ch"

function teste(e: int)
{
	if (e != 0)
		Application.Abort("errno "+e.toString());
}

println("creating file");
var file, e = File.Open("testfile.dat", "w+");
teste(e);
var outs = File.OutputStream(file, 0);
for i = 0, 10
	outs.writeByte(i);
file.close();

println("reading back file");
file, e = File.Open("testfile.dat", "r");
teste(e);
var ins = File.InputStream(file, 0);
while (true)
{
	var c = ins.readByte();
	if (c == -1)
		break;
		
	println("c = "+c.toString());
}
file.close();

println("removing file");
File.Remove("testfile.dat");

println("recreating file");
file, e = File.Open("testfile.dat", "w+");
teste(e);
{
	var outs = File.OutputStream(file, 0);
	var outw = OutputStreamWriter(outs);
	outw.writeCodePoint(99);
	outw.writeCodePoint(1234);
	outw.writeCodePoint(5678);
	outw.writeString("fnord\n");
	file.close();
}

println("reading back file");
file, e = File.Open("testfile.dat", "r");
teste(e);
{
	var ins = File.InputStream(file, 0);
	var inr = InputStreamReader(ins);
	var s = inr.readString();
	
	println("read <"+s+">");
}
file.close();

println("removing file, again");
File.Remove("testfile.dat");