# 2014 March 25.
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. 
#
# The tests in this file verify that sorting works when the library is
# configured to use mmap(), but the temporary files generated by the
# sorter are too large to be completely mapped.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix sort3

# Sort roughly 20MB of data. Once with a mmap limit of 5MB and once without.
#
foreach {itest limit} {
  1 5000000
  2 0x7FFFFFFF
} {
  sqlite3_test_control SQLITE_TESTCTRL_SORTER_MMAP db $limit
  do_execsql_test 1.$itest {
    WITH r(x,y) AS (
        SELECT 1, randomblob(1000)
        UNION ALL
        SELECT x+1, randomblob(1000) FROM r
        LIMIT 20000
    )
    SELECT count(*), sum(length(y)) FROM r GROUP BY (x%5);
  } {
    4000 4000000 
    4000 4000000 
    4000 4000000 
    4000 4000000 
    4000 4000000
  }
}

# Sort more than 2GB of data. At one point this was causing a problem.
# This test might take one minute or more to run.
#
do_execsql_test 2 {
  PRAGMA cache_size = 20000;
  WITH r(x,y) AS (
    SELECT 1, randomblob(1000)
    UNION ALL
    SELECT x+1, randomblob(1000) FROM r
    LIMIT 2200000
  )
  SELECT count(*), sum(length(y)) FROM r GROUP BY (x%5);
} {
  440000 440000000 
  440000 440000000 
  440000 440000000 
  440000 440000000 
  440000 440000000
}

finish_test

