From 96dd1e7944d24e762608a06eb48f59d5e3eae941 Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja.osipov@gmail.com>
Date: Mon, 12 Dec 2011 23:20:59 +0400
Subject: [PATCH] Lua: add tests for external iterators.

---
 test/box/lua.result         | 79 +++++++++++++++++++++++++++++++++++++
 test/box/lua.test           | 33 +++++++++++++++-
 test/box_big/tree_pk.result | 30 ++++++++++++++
 test/box_big/tree_pk.test   | 28 +++++++++++++
 4 files changed, 169 insertions(+), 1 deletion(-)

diff --git a/test/box/lua.result b/test/box/lua.result
index 878660e0dc..5ed71536b4 100644
--- a/test/box/lua.result
+++ b/test/box/lua.result
@@ -727,3 +727,82 @@ Found 1 tuple:
 call f1('jason', 1, 'test', 2, 'stewart')
 Found 1 tuple:
 ['jason', '1', 1953719668, '2', 'stewart']
+lua  function box.crossjoin(space0, space1, limit)   space0 = tonumber(space0)   space1 = tonumber(space1)   limit = tonumber(limit)   local result = {}   for k0, v0 in box.space[space0]:pairs() do     for k1, v1 in box.space[space1]:pairs() do       if limit <= 0 then         return unpack(result)       end       newtuple = {v0:unpack()}       for _, v in v1:pairs() do table.insert(newtuple, v) end       table.insert(result, newtuple)       limit = limit - 1     end   end   return unpack(result) end
+---
+...
+lua box.crossjoin(0, 0, 0)
+---
+...
+lua box.crossjoin(0, 0, 10000)
+---
+...
+lua box.space[0]:insert(1)
+---
+ - 1: {}
+...
+call box.crossjoin('0', '0', '10000')
+Found 1 tuple:
+[1, 1]
+lua box.space[0]:insert(2)
+---
+ - 2: {}
+...
+call box.crossjoin('0', '0', '10000')
+Found 4 tuples:
+[1, 1]
+[1, 2]
+[2, 1]
+[2, 2]
+lua box.space[0]:insert(3, 'hello')
+---
+ - 3: {'hello'}
+...
+call box.crossjoin('0', '0', '10000')
+Found 9 tuples:
+[1, 1]
+[1, 2]
+[1, 3, 'hello']
+[2, 1]
+[2, 2]
+[2, 3, 'hello']
+[3, 'hello', 1]
+[3, 'hello', 2]
+[3, 'hello', 3, 'hello']
+lua box.space[0]:insert(4, 'world')
+---
+ - 4: {'world'}
+...
+lua box.space[0]:insert(5, 'hello world')
+---
+ - 5: {'hello world'}
+...
+call box.crossjoin('0', '0', '10000')
+Found 25 tuples:
+[1, 1]
+[1, 2]
+[1, 3, 'hello']
+[1, 4, 'world']
+[1, 5, 'hello world']
+[2, 1]
+[2, 2]
+[2, 3, 'hello']
+[2, 4, 'world']
+[2, 5, 'hello world']
+[3, 'hello', 1]
+[3, 'hello', 2]
+[3, 'hello', 3, 'hello']
+[3, 'hello', 4, 'world']
+[3, 'hello', 5, 'hello world']
+[4, 'world', 1]
+[4, 'world', 2]
+[4, 'world', 3, 'hello']
+[4, 'world', 4, 'world']
+[4, 'world', 5, 'hello world']
+[5, 'hello world', 1]
+[5, 'hello world', 2]
+[5, 'hello world', 3, 'hello']
+[5, 'hello world', 4, 'world']
+[5, 'hello world', 5, 'hello world']
+lua box.space[0]:truncate()
+---
+...
diff --git a/test/box/lua.test b/test/box/lua.test
index 9ae9819358..0a856b8b42 100644
--- a/test/box/lua.test
+++ b/test/box/lua.test
@@ -202,4 +202,35 @@ exec sql "call f1()"
 exec sql "call f2()"
 exec sql "call f1('jason')"
 exec sql "call f1('jason', 1, 'test', 2, 'stewart')"
-
+lua = """
+function box.crossjoin(space0, space1, limit)
+  space0 = tonumber(space0)
+  space1 = tonumber(space1)
+  limit = tonumber(limit)
+  local result = {}
+  for k0, v0 in box.space[space0]:pairs() do
+    for k1, v1 in box.space[space1]:pairs() do
+      if limit <= 0 then
+        return unpack(result)
+      end
+      newtuple = {v0:unpack()}
+      for _, v in v1:pairs() do table.insert(newtuple, v) end
+      table.insert(result, newtuple)
+      limit = limit - 1
+    end
+  end
+  return unpack(result)
+end"""
+exec admin "lua " + lua.replace('\n', ' ')
+exec admin "lua box.crossjoin(0, 0, 0)"
+exec admin "lua box.crossjoin(0, 0, 10000)"
+exec admin "lua box.space[0]:insert(1)"
+exec sql "call box.crossjoin('0', '0', '10000')"
+exec admin "lua box.space[0]:insert(2)"
+exec sql "call box.crossjoin('0', '0', '10000')"
+exec admin "lua box.space[0]:insert(3, 'hello')"
+exec sql "call box.crossjoin('0', '0', '10000')"
+exec admin "lua box.space[0]:insert(4, 'world')"
+exec admin "lua box.space[0]:insert(5, 'hello world')"
+exec sql "call box.crossjoin('0', '0', '10000')"
+exec admin "lua box.space[0]:truncate()"
diff --git a/test/box_big/tree_pk.result b/test/box_big/tree_pk.result
index 4a3e468081..319e07a0bb 100644
--- a/test/box_big/tree_pk.result
+++ b/test/box_big/tree_pk.result
@@ -63,3 +63,33 @@ delete from t3 where k0 = 'second'
 Delete OK, 1 row affected
 delete from t3 where k0 = 'third'
 Delete OK, 1 row affected
+insert into t2 values (1, 'tuple')
+Insert OK, 1 row affected
+insert into t3 values (1, 'tuple')
+Insert OK, 1 row affected
+insert into t3 values (2, 'tuple')
+Insert OK, 1 row affected
+lua  function box.crossjoin(space0, space1, limit)   space0 = tonumber(space0)   space1 = tonumber(space1)   limit = tonumber(limit)   local result = {}   for k0, v0 in box.space[space0]:pairs() do     for k1, v1 in box.space[space1]:pairs() do       if limit <= 0 then         return unpack(result)       end       newtuple = {v0:unpack()}       for _, v in v1:pairs() do table.insert(newtuple, v) end       table.insert(result, newtuple)       limit = limit - 1     end   end   return unpack(result) end
+---
+...
+call box.crossjoin(3, 3, 0)
+No match
+call box.crossjoin(3, 3, 5)
+Found 4 tuples:
+[1, 'tuple', 1, 'tuple']
+[1, 'tuple', 2, 'tuple']
+[2, 'tuple', 1, 'tuple']
+[2, 'tuple', 2, 'tuple']
+call box.crossjoin(3, 3, 10000)
+Found 4 tuples:
+[1, 'tuple', 1, 'tuple']
+[1, 'tuple', 2, 'tuple']
+[2, 'tuple', 1, 'tuple']
+[2, 'tuple', 2, 'tuple']
+call box.crossjoin(3, 2, 10000)
+Found 2 tuples:
+[1, 'tuple', 1, 'tuple']
+[2, 'tuple', 1, 'tuple']
+lua box.space[3]:truncate()
+---
+...
diff --git a/test/box_big/tree_pk.test b/test/box_big/tree_pk.test
index 3792956c9f..090a90182a 100644
--- a/test/box_big/tree_pk.test
+++ b/test/box_big/tree_pk.test
@@ -32,3 +32,31 @@ exec sql "select * from t3 where k0 = 'third'"
 exec sql "delete from t3 where k0 = 'identifier'"
 exec sql "delete from t3 where k0 = 'second'"
 exec sql "delete from t3 where k0 = 'third'"
+lua = """
+function box.crossjoin(space0, space1, limit)
+  space0 = tonumber(space0)
+  space1 = tonumber(space1)
+  limit = tonumber(limit)
+  local result = {}
+  for k0, v0 in box.space[space0]:pairs() do
+    for k1, v1 in box.space[space1]:pairs() do
+      if limit <= 0 then
+        return unpack(result)
+      end
+      newtuple = {v0:unpack()}
+      for _, v in v1:pairs() do table.insert(newtuple, v) end
+      table.insert(result, newtuple)
+      limit = limit - 1
+    end
+  end
+  return unpack(result)
+end"""
+exec sql "insert into t2 values (1, 'tuple')"
+exec sql "insert into t3 values (1, 'tuple')"
+exec sql "insert into t3 values (2, 'tuple')"
+exec admin "lua " + lua.replace('\n', ' ')
+exec sql "call box.crossjoin(3, 3, 0)"
+exec sql "call box.crossjoin(3, 3, 5)"
+exec sql "call box.crossjoin(3, 3, 10000)"
+exec sql "call box.crossjoin(3, 2, 10000)"
+exec admin "lua box.space[3]:truncate()"
-- 
GitLab