#!/usr/bin/env ascript
# -*- coding: shift_jis -*-
sys.echo(false)
tester(test:expr):map:dynamic_scope = printf("%s .. %s\n", test, eval(test)):nomap

sys.stdout.setencoding("shift_jis")

lambda {
	codec('shift_jis').encode('あいうえお').dump()
	codec('shift_jis').decode('\x82\xa0\x82\xa2\x82\xa4\x82\xa6\x82\xa8').println()
	tester(`hex('あいうえおかきくけこさしすせそたちつてとなにぬねの'.each():utf32):list)
}()
lambda {
	tester(`"x < 0 & y > 1".escapehtml());
	tester(`"x &lt; 0 &amp; y &gt; 1".unescapehtml());
	tester(`"&quot;hello world&quot;".unescapehtml());
}()
lambda {
	str = "hello world"
	tester(`str.align(16):center)
	tester(`str.align(16):left)
	tester(`str.align(16):right)
	tester(`str.align(16, "*"):center)
	tester(`str.align(16, "*"):left)
	tester(`str.align(16, "*"):right)
	import(string) { align }
	tester(`align(str, 16):center)
	tester(`align(str, 16):left)
	tester(`align(str, 16):right)
	tester(`align(str, 16, "*"):center)
	tester(`align(str, 16, "*"):left)
	tester(`align(str, 16, "*"):right)
}()
lambda {
	str = "   hello world   "
	tester(`format("[%s]", str.strip():both))
	tester(`format("[%s]", str.strip():left))
	tester(`format("[%s]", str.strip():right))
	import(string) { strip }
	tester(`format("[%s]", strip(str):both))
	tester(`format("[%s]", strip(str):left))
	tester(`format("[%s]", strip(str):right))
}()
lambda {
	var = "HOGE"
	tester(`e"hello ${var} world")
	tester(`e"hello ${var * 2} world")
	tester(`e"""hello "${format("%8s", var)}" world""")
	var = 3.14
	tester(`e"hello ${var} world")
}()
lambda {
	print(open("chars-cp932.txt", "r", "shift_jis").readlines())
	print(open("chars-utf8-jp.txt", "r", "utf-8").readlines())
	print(open("chars-eucjp.txt", "r", "euc-jp").readlines())
	print(open("chars-jis.txt", "r", "jis").readlines())
}()
lambda {
	str = "first line\nsecond line\nthird line\nfourth line\nfifth line\n"
	tester(`str)
	tester(`str.eachline():list)
	tester(`str.eachline(2):list)
	tester(`string.eachline(str):list)
	tester(`string.eachline(str, 2):list)
	tester(`str.eachline():list:chop)
	tester(`str.eachline(2):list:chop)
	tester(`string.eachline(str):list:chop)
	tester(`string.eachline(str, 2):list:chop)
	str = "first line\r\nsecond line\r\nthird line\r\nfourth line\r\nfifth line\r\n"
	tester(`str)
	tester(`str.eachline():list)
	tester(`str.eachline(2):list)
	tester(`string.eachline(str):list)
	tester(`string.eachline(str, 2):list)
	tester(`str.eachline():list:chop)
	tester(`str.eachline(2):list:chop)
	tester(`string.eachline(str):list:chop)
	tester(`string.eachline(str, 2):list:chop)
}()
lambda {
	tester(`chr(0x00..0x7f):list)
	tester(`ord(chr(0x00..0x7f)):list)
	tester(`chr(ord("あいうえお".split())):list)
}()
lambda {
	str = "あいうえお"
	tester(`str)
	tester(`str.len())
	tester(`str.left(0))
	tester(`str.left(1))
	tester(`str.left(2))
	tester(`str.left(3))
	tester(`str.left(4))
	tester(`str.left(5))
	tester(`str.left(6))
	tester(`str.left(10))
	tester(`str.right(0))
	tester(`str.right(1))
	tester(`str.right(2))
	tester(`str.right(3))
	tester(`str.right(4))
	tester(`str.right(5))
	tester(`str.right(6))
	tester(`str.right(10))
	tester(`str.mid(2, 1))
	tester(`str.mid(2, 2))
	tester(`str.mid(2, 3))
	tester(`str.mid(2, 4))
	tester(`str.mid(1, 4))
	tester(`str.mid(1, 10))
	tester(`str.mid(pos => 1, len => 2))
	tester(`str.mid(len => 2, pos => 1))
	tester(`str.mid(len => 2))
	tester(`str.mid(0))
	tester(`str.mid(1))
	tester(`str.mid(3))
	tester(`str.mid(-6))
	tester(`str.mid(-5))
	tester(`str.mid(-4))
	tester(`str.mid(-3))
	tester(`str.mid(-2))
	tester(`str.mid(-1))
	tester(`str.startswith('あいう'))
	tester(`str.startswith('かきく'))
	tester(`str.endswith('うえお'))
	tester(`str.endswith('くけこ'))
	tester(`str.startswith('うえ', 2))
	tester(`str.endswith('うえ', 4))
}()
lambda {
	str = "abcdefghijklmn"
	tester(`str)
	tester(`str.startswith('abcd'))
	tester(`str.startswith('abdd'))
	tester(`str.startswith('ABCD'))
	tester(`str.startswith('ABCD'):icase)
	tester(`str.endswith('klmn'))
	tester(`str.endswith('klmm'))
	tester(`str.endswith('KLMN'))
	tester(`str.endswith('KLMN'):icase)
	tester(`str.startswith('defg', 3))
	tester(`str.endswith('ghij', 10))
}()
lambda {
	tester(`string.split("apple[sep]orange[sep]pear[sep]orange[sep]banana", "[sep]"):list)
	tester(`"apple[sep]orange[sep]pear[sep]orange[sep]banana".split("[sep]"):list)
	tester(`"apple|orange|pear|orange|banana".split("|"):list)
	tester(`"|apple|orange||pear|orange|banana|".split("|"):list)
	tester(`"".split("|"):list)
	tester(`string.join(["apple", "orange", "pear", "orange", "banana"], ";"))
	tester(`["apple", "orange", "pear", "orange", "banana"].join(";"))
}()
lambda {
	println("日本語SJISテスト")
	println("構わない .. contains 0x5c (\\)")
	println("芸能界 .. contains 0x5c (\\)")
	println("""芸能界""")
	print("""this is the first line
2nd line
3rd line
4th line
5th line
6th line
""")
	print("""\
this is the first line
2nd line
3rd line
4th line
5th line
6th line
""")
	println(r"raw mode \n \t \r \\ \"")
	println(R"raw mode \n \t \r \\ \"")
	println(r"""raw mode \n \t \r \\ \"""")
	println(R"""raw mode \n \t \r \\ \"""")
	println(r"regular expression: (\w+)\((\w+)(\w+)\)")
	println("string concatenation test: " " 1st" " 2dn" " 3rd" r" 4th" R" 5th" \
			" 6th" """ 7th""" r""" 8th""")
}()
lambda {
	println('日本語SJISテスト')
	println('構わない .. contains 0x5c (\\)')
	println('芸能界 .. contains 0x5c (\\)')
	println('''芸能界''')
	print('''this is the first line
2nd line
3rd line
4th line
5th line
6th line
''')
	print('''\
this is the first line
2nd line
3rd line
4th line
5th line
6th line
''')
	println(r'raw mode \n \t \r \\ \'')
	println(R'raw mode \n \t \r \\ \'')
	println(r'''raw mode \n \t \r \\ \'''')
	println(R'''raw mode \n \t \r \\ \'''')
	println(r'regular expression: (\w+)\((\w+)(\w+)\)')
	println('string concatenation test: ' ' 1st' ' 2dn' ' 3rd' r' 4th' R' 5th' \
			' 6th' ''' 7th''' r''' 8th''')
}()
lambda {
	tester([`(math.sqrt * 2), `(math.sin * math.pi), `(math.cos * math.pi)])
	tester(`string.find("hello world hello hello", "llo"))
	tester(`string.find("hello world hello hello", "llo"):rev)
	tester(`string.find("hello world hello hello", "llo"):list)
	tester(`string.find("hello world hello hello", "Llo"))
	tester(`string.find("hello world hello hello", "Llo"):rev)
	tester(`string.find("hello world hello hello", "Llo"):list)
	tester(`string.find("hello world hello hello", "Llo"):icase)
	tester(`string.find("hello world hello hello", "Llo"):rev:icase)
	tester(`string.find("hello world hello hello", "Llo"):list:icase)
}()
lambda {
	tester([`(math.sqrt * 2), `(math.sin * math.pi), `(math.cos * math.pi)])
	tester(`"hello world hello hello".find("llo"))
	tester(`"hello world hello hello".find("llo"):rev)
	tester(`"hello world hello hello".find("llo"):list)
	tester(`"hello world hello hello".find("Llo"))
	tester(`"hello world hello hello".find("Llo"):rev)
	tester(`"hello world hello hello".find("Llo"):list)
	tester(`"hello world hello hello".find("Llo"):icase)
	tester(`"hello world hello hello".find("Llo"):rev:icase)
	tester(`"hello world hello hello".find("Llo"):list:icase)
}()
lambda {
	str = "hello world"
	tester(`str)
	tester([`string.left(str, nil), `string.left(str, 5)])
	tester([`string.right(str, nil), `string.right(str, 5)])
	tester([`string.mid(str, nil, nil), `string.mid(str, 2, nil),
			`string.mid(str, nil, 5), `string.mid(str, 2, 3)])
	tester(`string.replace(str, "ll", "LL"))
	tester(`string.replace(str, "o", "[O]"))
	tester(`string.replace(str, "o", "[O]", 0))
	tester(`string.replace(str, "o", "[O]", 1))
	tester(`string.replace(str, "", "*"))
	tester(`string.replace(str, "", "*", 0))
	tester(`string.replace(str, "", "*", 3))
}()
lambda {
	str = "hello world"
	tester(`str)
	tester([`str.left(nil), `str.left(5)])
	tester([`str.right(nil), `str.right(5)])
	tester([`str.mid(nil, nil), `str.mid(2, nil), `str.mid(nil, 5), `str.mid(2, 3)])
	tester(`str.replace("ll", "LL"))
	tester(`str.replace("o", "[O]"))
	tester(`str.replace("o", "[O]", 0))
	tester(`str.replace("o", "[O]", 1))
	tester(`str.replace("", "*"))
	tester(`str.replace("", "*", 0))
	tester(`str.replace("", "*", 3))
}()
lambda {
	x = @(struct(a, b, c, d, e):loose) {
		[1, 2, 3, 4, 5],
		[3, 4, 2, 1],
		[2, 3, 4],
		[1, 1],
		[1]
	}
	lambda(x):map { printf("%d %d %d %d %d\n", x.a, x.b, x.c, x.d, x.e) }(x)
}()
