/*
 * Copyright 2009-2010 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;

import net.morilib.awk.code.AwkExecutable;

/**
 *
 *
 * @author MORIGUCHI, Yuichiro 2013/03/09
 */
public class AwkProgramTest extends AwkTestCase {

	public void testProgram0001() {
		AwkExecutable e = pre("{ print; }");

		e.execute(ns0, fs0, "aaaa");
		eot("aaaa");
	}

	public void testProgram0002() {
		AwkExecutable e = pre("{ print $1, $2; }");

		e.execute(ns0, fs0, "765 876 961");
		eot("765 876");
	}

	public void testProgram0003() {
		AwkExecutable e = pre("{ print }");

		e.execute(ns0, fs0, "aaaa");
		eot("aaaa");
	}

	public void testProgram0004() {
		AwkExecutable e = pre("" +
				"#aaaaaa\n" +
				"{ print }  # aaaa\n");

		e.execute(ns0, fs0, "aaaa");
		eot("aaaa");
	}

	public void testProgram0005() {
		AwkExecutable e = pre("" +
				"{\n" +
				"  print # aaaa\n" +
				"}");

		e.execute(ns0, fs0, "aaaa");
		eot("aaaa");
	}

	public void testProgram0006() {
		AwkExecutable e = pre("" +
				"{\n" +
				"  print;# aaaa\n" +
				"}");

		e.execute(ns0, fs0, "aaaa");
		eot("aaaa");
	}

	public void testProgram0011() {
		AwkExecutable e = pre("/a+/ { print; }");

		e.execute(ns0, fs0, "aaaa");
		e.execute(ns0, fs0, "bbbb");
		e.execute(ns0, fs0, "abba");
		eot("aaaa");
		eot("abba");
	}

	public void testProgram0012() {
		AwkExecutable e = pre("/a+/, /b+/ { print; }");

		e.execute(ns0, fs0, "bbbb");
		e.execute(ns0, fs0, "aaaa");
		e.execute(ns0, fs0, "dddd");
		e.execute(ns0, fs0, "cccc");
		e.execute(ns0, fs0, "abba");
		e.execute(ns0, fs0, "abba");
		eot("aaaa");
		eot("dddd");
		eot("cccc");
		eot("abba");
		eot();
	}

	public void testProgram0013() {
		AwkExecutable e = pre("$1 ~ /a+/ { print }");

		e.execute(ns0, fs0, "aaaa");
		e.execute(ns0, fs0, "bbbb");
		e.execute(ns0, fs0, "abba");
		eot("aaaa");
		eot("abba");
	}

	public void testProgram0014() {
		AwkExecutable e = pre("$1 ~ /a+/");

		e.execute(ns0, fs0, "aaaa");
		e.execute(ns0, fs0, "bbbb");
		e.execute(ns0, fs0, "abba");
		eot("aaaa");
		eot("abba");
	}

	public void testProgram0021() {
		AwkExecutable e = pre("" +
				"function aaa() { print; }" +
				"{ aaa(); }");

		e.execute(ns0, fs0, "aaaa");
		eot("aaaa");
	}

	public void testProgram0022() {
		AwkExecutable e = pre("" +
				"function aaa(aaa) { print aaa; }" +
				"{ aaa($0); aaa($0); }");

		e.execute(ns0, fs0, "aaaa");
		eot("aaaa");
		eot("aaaa");
	}

	public void testProgram0023() {
		AwkExecutable e = pre("" +
				"function aaa(a, b) { print a, b; }" +
				"{ aaa($1, $2); }");

		e.execute(ns0, fs0, "aaa bbb");
		eot("aaa bbb");
	}

	public void testProgram0024() {
		AwkExecutable e = pre("" +
				"function aaa() { print a++; }" +
				"{ aaa(); print a; }");

		e.execute(ns0, fs0, "aaa bbb");
		e.execute(ns0, fs0, "aaa bbb");
		e.execute(ns0, fs0, "aaa bbb");
		eot("0");
		eot("1");
		eot("1");
		eot("2");
		eot("2");
		eot("3");
	}

	public void testProgram0025() {
		AwkExecutable e = pre("" +
				"function aaa(a) { print a++; }" +
				"{ aaa(1); print a; }");

		e.execute(ns0, fs0, "aaa bbb");
		eot("1");
		eot("");
	}

	public void testProgram0031() {
		AwkExecutable e = pre("" +
				"BEGIN { a = 1; }\n" +
				"{ print a; }\n" +
				"END { print 2; }");

		e.executeBegin(ns0, fs0);
		e.execute(ns0, fs0, "aaaa");
		e.executeEnd(ns0, fs0);
		eot("1");
		eot("2");
	}

	public void testProgram0041() {
		AwkExecutable e = pre("{ print $1, $2, NF; }");

		e.execute(ns0, fs0, "\t765 \t876  \t961  ");
		eot("765 876 3");
	}

	public void testProgram0042() {
		AwkExecutable e = pre("" +
				"BEGIN { FS = \"|\"; }\n" +
				"{ print $1, $2, NF; }");

		e.executeBegin(ns0, fs0);
		e.execute(ns0, fs0, "765|876|961");
		eot("765 876 3");
	}

	public void testProgram0043() {
		AwkExecutable e = pre("" +
				"BEGIN { FS = \"|\"; }\n" +
				"{ print $1, $2, $3, $4, NF; }");

		e.executeBegin(ns0, fs0);
		e.execute(ns0, fs0, "|765||876|961");
		eot(" 765  876 5");
	}

	public void testProgram0044() {
		AwkExecutable e = pre("" +
				"BEGIN { FS = \"[:;]+\"; }\n" +
				"{ print $1, $2, NF; }");

		e.executeBegin(ns0, fs0);
		e.execute(ns0, fs0, "765:;876;961::");
		eot("765 876 3");
	}

	public void testProgram0045() {
		AwkExecutable e = pre("" +
				"BEGIN { FS = \"[:;]+\"; }\n" +
				"{ print $1, $2, NF; }");

		e.executeBegin(ns0, fs0);
		e.execute(ns0, fs0, ":::765:;876;961::");
		eot("765 876 3");
	}

	public void testProgram0051() {
		AwkExecutable e = pre("" +
				"{ print FNR, NR }\n" +
				"END { print FNR, NR }");

		e.execute(ns0, fs0, "aaaa");
		e.execute(ns0, fs0, "aaaa");
		e.execute(ns0, fs0, "aaaa");
		e.executeEnd(ns0, fs0);
		eot("1 1");
		eot("2 2");
		eot("3 3");
		eot("3 3");
	}

	public void testProgram0061() {
		AwkExecutable e = pre("" +
				"function aaa(a) { return a*a }" +
				"{ print aaa(NR); }");

		e.execute(ns0, fs0, "aaa bbb");
		e.execute(ns0, fs0, "aaa bbb");
		eot("1");
		eot("4");
	}

	public void testProgram0071() {
		AwkExecutable e = pre("/aaa/");

		e.execute(ns0, fs0, "aaaa");
		e.execute(ns0, fs0, "aabb");
		e.execute(ns0, fs0, "aaab");
		eot("aaaa");
		eot("aaab");
	}

	public void testProgram0081() {
		AwkExecutable e = pre("{ print $1 $2; }");

		e.execute(ns0, fs0, "765 876 961");
		eot("765876");
	}

	public void testProgram0082() {
		AwkExecutable e = pre("{ print $1 !$2; }");

		e.execute(ns0, fs0, "765 876 961");
		eot("7650");
	}

}
