#!/usr/bin/env ascript
tester(test:expr):map:dynamic_scope = printf("%s .. %s\n", test, eval(test)):nomap

lambda {
	dict = %{}
	dict[0..10] = 0..10
	tester(`dict)
	dict[0..10] = 'hello'
	tester(`dict)
}()
lambda {
	dict = %{
		`hoge	=>	"aaaaaa"
		`foo	=>	"bbbbb"
	}
	dict.store {`bar => "cccccc"}
	tester(`dict)
	dict.store {`hoge => "AAAAAA"}
	tester(`dict)
	dict.store([`hoge1, "ddddd", `hoge2, "eeeee"])
	tester(`dict)
	dict.store([[`hoge3, "fffff"], [`hoge4, "ggggg"]])
	tester(`dict)
	dict.store([`hoge1, "DDDDD", `hoge2, "EEEEE"])
	tester(`dict)
	dict.store([[`hoge3, "FFFFF"], [`hoge4, "GGGGG"]])
	tester(`dict)
	dict.store(%{`hoge3 => "*FFFFF*", `hoge4 => "*GGGGG*"})
	tester(`dict)
}()
lambda {
	dict = %{
		`hoge	=>	"aaaaaa"
		`foo	=>	"bbbbb"
		`aaaa	=>	"AAAAAAA"
		`bbbb	=>	"BBBBBBB"
		`cccc	=>	"CCCCCCC"
		`dddd	=>	"DDDDDDD"
		`eeee	=>	"EEEEEEE"
		`ffff	=>	"FFFFFFF"
	}
	tester(`dict.get([`hoge, `foo, `aaaa, `bbbb, `cccc, `dddd, `eeee, `ffff, `eee]))
	tester(`dict.get([`hoge, `foo, `aaaa, `bbbb, `cccc, `dddd, `eeee, `ffff, `eee], [1, 2, 3]))
	tester(`dict.gets([`hoge, `foo, `aaaa, `Hoge, `Foo, `Aaaa], [1, 2, 3, 1, 2, 3]))
}()
lambda {
	d = %{}
	d.set([1, 2, 3], [4, 5, 6])
	tester(`d)
	d = %{}
	d.sets([1, 2, 3], [4, 5, 6])
	tester(`d)
}()
lambda {
	PrintDict(dict) = {
		println("items      = ", dict.items():list):nomap
		println("keys       = ", dict.keys():list):nomap
		println("values     = ", dict.values():list):nomap
		println("dict[keys] = ", dict[dict.keys()]):nomap
	}
	dict = %{
		`hoge	=>	"aaaaaa"
		`foo	=>	"bbbbb"
		`aaaa	=>	"AAAAAAA"
		`bbbb	=>	"BBBBBBB"
		`cccc	=>	"CCCCCCC"
		`dddd	=>	"DDDDDDD"
		`eeee	=>	"EEEEEEE"
		`ffff	=>	"FFFFFFF"
	}
	PrintDict(dict)
	try {
		dict = %{
			`hoge	=>	"aaaaaa"
			`foo	=>	"bbbbb"
			`hoge	=>	"AAAA"		# causes error
		}
	} except(KeyError) {|e|
		println("expected error occurs: ", e.text)
	}
	println("for loop")
	for ([key, value] in dict.items()) {
		printf("%-10s %s\n", key, value)
	}
	println("implicit mapping with keys() and values()")
	printf("%-10s %s\n", dict.keys(), dict.values())
	println("member mapping with items()")
	dict.items().printf("%-10s %s\n")
}()
lambda {
	nestedDict = %{
		"key-1", %{
			"key-1-1" => "value of 1-1"
			"key-1-2" => "value of 1-2"
			"key-1-3" => "value of 1-3"
			"key-1-4" => "value of 1-4"
			"key-1-5" => "value of 1-5"
			"key-1-6" => "value of 1-6"
		}
		"key-2", %{
			"key-2-1" => "value of 2-1"
			"key-2-2" => "value of 2-2"
			"key-2-3" => "value of 2-3"
			"key-2-4" => "value of 2-4"
			"key-2-5" => "value of 2-5"
		}
		"key-3", %{
			"key-3-1" => "value of 3-1"
			"key-3-2" => "value of 3-2"
			"key-3-3" => "value of 3-3"
			"key-3-4" => "value of 3-4"
			"key-3-5" => "value of 3-5"
			"key-3-6" => "value of 3-6"
			"key-3-7" => "value of 3-7"
			"key-3-8" => "value of 3-8"
			"key-3-9" => "value of 3-9"
		}
	}
	sys.echo(true)
	for (test in (`nestedDict["key-1"]["key-1-1"], `nestedDict["key-3"]["key-3-1"])) {
		print(test, " .. ", eval(test), "\n")
	}
	println("--------")
	for (dict in nestedDict[nestedDict.keys()]) {
		for (value in dict[dict.keys()]) { print(" ", value) }
		println("")
	}
}()
lambda {
	dict = %{
		`key1 => "value of key1"
		`key2 => "value of key2"
		`key3 => "value of key3"
		`key4 => "value of key4"
		`key5 => "value of key5"
		`key6 => "value of key6"
		`key7 => "value of key7"
	}
	print(dict), println()
	println(dict.haskey(`key1, `key2, `key3, `key4, `key5,
								`key6, `key7, `key0, `key8).join(" "))
}()
