/*
 * Copyright 2013 Yuichiro Moriguchi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.morilib.awk;

public class ExtendAwkIOTest extends AwkTestCase {

	public void testPrint01() {
		exe("print 1 > \"/dev/stdout\";");
		eot("1");
		exe("print 1, 2, 3 > \"/dev/stdout\";");
		eot("1 2 3");
		exe("print(1, 2, 3) > \"/dev/stdout\";");
		eot("1 2 3");
		exe("print 1 + 2 > \"/dev/stdout\";");
		eot("3");
		exe("print 1 + 2, 3 > \"/dev/stdout\";");
		eot("3 3");
	}

	public void testPrint01fd() {
		exe("print 1 > \"/dev/fd/1\";");
		eot("1");
		exe("print 1, 2, 3 > \"/dev/fd/1\";");
		eot("1 2 3");
		exe("print(1, 2, 3) > \"/dev/fd/1\";");
		eot("1 2 3");
		exe("print 1 + 2 > \"/dev/fd/1\";");
		eot("3");
		exe("print 1 + 2, 3 > \"/dev/fd/1\";");
		eot("3 3");
	}

	public void testPrint02() {
		exe("print 10 > \"/dev/stderr\";");
		eet("10");
		exe("print 7, 6, 5 > \"/dev/stderr\";");
		eet("7 6 5");
		exe("print 7, 6, (5 > \"/dev/stderr\");");
		eot("7 6 1");
		exe("print(7, 6, 5) > \"/dev/stderr\";");
		eet("7 6 5");
	}

	public void testPrint02fd() {
		exe("print 10 > \"/dev/fd/2\";");
		eet("10");
		exe("print 7, 6, 5 > \"/dev/fd/2\";");
		eet("7 6 5");
		exe("print 7, 6, (5 > \"/dev/fd/2\");");
		eot("7 6 1");
		exe("print(7, 6, 5) > \"/dev/fd/2\";");
		eet("7 6 5");
	}

	public void testPrint03() {
		exe("print 10 >> \"/dev/stderr\";");
		eet("10");
		exe("print 7, 6, 5 >> \"/dev/stderr\";");
		eet("7 6 5");
		exe("print(7, 6, 5) >> \"/dev/stderr\";");
		eet("7 6 5");
	}

	public void testPrintf01() {
		exe("printf \"1\\n\" > \"/dev/stdout\";");
		eot("1");
		exe("printf \"%02d\\n\", 1 > \"/dev/stdout\";");
		eot("01");
		exe("printf(\"%02d\\n\", 1) > \"/dev/stdout\";");
		eot("01");
		exe("printf \"%02d %4s\\n\", 1, \"765\" > \"/dev/stdout\";");
		eot("01  765");
	}

	public void testPrintf02() {
		exe("printf \"1\\n\" > \"/dev/stderr\";");
		eet("1");
		exe("printf \"%02d\\n\", 1 > \"/dev/stderr\";");
		eet("01");
		exe("printf(\"%02d\\n\", 1) > \"/dev/stderr\";");
		eet("01");
		exe("printf \"%02d %4s\\n\", 1, \"765\" > \"/dev/stderr\";");
		eet("01  765");
		exe("printf \"%02d\\n\", (1 > \"/dev/stderr\");");
		eot("01");
	}

	public void testPrintf03() {
		exe("printf \"1\\n\" >> \"/dev/stderr\";");
		eet("1");
		exe("printf \"%02d\\n\", 1 >> \"/dev/stderr\";");
		eet("01");
		exe("printf(\"%02d\\n\", 1) >> \"/dev/stderr\";");
		eet("01");
		exe("printf \"%02d %4s\\n\", 1, \"765\" >> \"/dev/stderr\";");
		eet("01  765");
	}

	public void testGetline02() {
		piw.println("7 6 5");
		piw.println("8 7 6");
		piw.close();
		eqi("getline < \"/dev/stdin\";", 1);
		eqs("$1;", "7");
		eqi("getline ai < \"/dev/stdin\";", 1);
		eqs("ai;", "8 7 6");
		eqi("getline < \"/dev/stdin\";", 0);
	}

	public void testGetline02fd() {
		piw.println("7 6 5");
		piw.println("8 7 6");
		piw.close();
		eqi("getline < \"/dev/fd/0\";", 1);
		eqs("$1;", "7");
		eqi("getline ai < \"/dev/fd/0\";", 1);
		eqs("ai;", "8 7 6");
		eqi("getline < \"/dev/fd/0\";", 0);
	}

	public void testNull01() {
		exe("print 1 > \"/dev/null\";");
		eot();
		eet();
	}

}
