000001  # 2013-12-17
000002  #
000003  # The author disclaims copyright to this source code.  In place of
000004  # a legal notice, here is a blessing:
000005  #
000006  #    May you do good and not evil.
000007  #    May you find forgiveness for yourself and forgive others.
000008  #    May you share freely, never taking more than you give.
000009  #
000010  #***********************************************************************
000011  # This file implements regression tests for SQLite library.  The
000012  # focus of this file is testing the printf() SQL function.
000013  #
000014  #
000015  # EVIDENCE-OF: R-63057-40065 The printf(FORMAT,...) SQL function works
000016  # like the sqlite3_mprintf() C-language function and the printf()
000017  # function from the standard C library.
000018  #
000019  
000020  set testdir [file dirname $argv0]
000021  source $testdir/tester.tcl
000022  
000023  # EVIDENCE-OF: R-40086-60101 If the FORMAT argument is missing or NULL
000024  # then the result is NULL.
000025  #
000026  do_execsql_test printf2-1.1 {
000027    SELECT quote(printf()), quote(printf(NULL,1,2,3));
000028  } {NULL NULL}
000029  
000030  
000031  do_execsql_test printf2-1.2 {
000032    SELECT printf('hello');
000033  } {hello}
000034  do_execsql_test printf2-1.3 {
000035    SELECT printf('%d,%d,%d',55,-11,3421);
000036  } {55,-11,3421}
000037  do_execsql_test printf2-1.4 {
000038    SELECT printf('%d,%d,%d',55,'-11',3421);
000039  } {55,-11,3421}
000040  do_execsql_test printf2-1.5 {
000041    SELECT printf('%d,%d,%d,%d',55,'-11',3421);
000042  } {55,-11,3421,0}
000043  do_execsql_test printf2-1.6 {
000044    SELECT printf('%.2f',3.141592653);
000045  } {3.14}
000046  do_execsql_test printf2-1.7 {
000047    SELECT printf('%.*f',2,3.141592653);
000048  } {3.14}
000049  do_execsql_test printf2-1.8 {
000050    SELECT printf('%*.*f',5,2,3.141592653);
000051  } {{ 3.14}}
000052  do_execsql_test printf2-1.9 {
000053    SELECT printf('%d',314159.2653);
000054  } {314159}
000055  do_execsql_test printf2-1.10 {
000056    SELECT printf('%lld',314159.2653);
000057  } {314159}
000058  do_execsql_test printf2-1.11 {
000059    SELECT printf('%lld%n',314159.2653,'hi');
000060  } {314159}
000061  do_execsql_test printf2-1.12 {
000062    SELECT printf('%n',0);
000063  } {{}}
000064  
000065  # EVIDENCE-OF: R-17002-27534 The %z format is interchangeable with %s.
000066  #
000067  do_execsql_test printf2-1.12 {
000068    SELECT printf('%.*z',5,'abcdefghijklmnop');
000069  } {abcde}
000070  do_execsql_test printf2-1.13 {
000071    SELECT printf('%c','abcdefghijklmnop');
000072  } {a}
000073  
000074  # EVIDENCE-OF: R-02347-27622 The %n format is silently ignored and does
000075  # not consume an argument.
000076  #
000077  do_execsql_test printf2-2.1 {
000078    CREATE TABLE t1(a,b,c);
000079    INSERT INTO t1 VALUES(1,2,3);
000080    INSERT INTO t1 VALUES(-1,-2,-3);
000081    INSERT INTO t1 VALUES('abc','def','ghi');
000082    INSERT INTO t1 VALUES(1.5,2.25,3.125);
000083    SELECT printf('(%s)-%n-(%s)',a,b,c) FROM t1 ORDER BY rowid;
000084  } {(1)--(2) (-1)--(-2) (abc)--(def) (1.5)--(2.25)}
000085  
000086  # EVIDENCE-OF: R-56064-04001 The %p format is an alias for %X.
000087  #
000088  do_execsql_test printf2-2.2 {
000089    SELECT printf('%s=(%p)',a,a) FROM t1 ORDER BY a;
000090  } {-1=(FFFFFFFFFFFFFFFF) 1=(1) 1.5=(1) abc=(0)}
000091  
000092  # EVIDENCE-OF: R-29410-53018 If there are too few arguments in the
000093  # argument list, missing arguments are assumed to have a NULL value,
000094  # which is translated into 0 or 0.0 for numeric formats or an empty
000095  # string for %s.
000096  #
000097  do_execsql_test printf2-2.3 {
000098    SELECT printf('%s=(%d/%g/%s)',a) FROM t1 ORDER BY a;
000099  } {-1=(0/0/) 1=(0/0/) 1.5=(0/0/) abc=(0/0/)}
000100  
000101  # The precision of the %c conversion causes the character to repeat.
000102  #
000103  do_execsql_test printf2-3.1 {
000104    SELECT printf('|%110.100c|','*');
000105  } {{|          ****************************************************************************************************|}}
000106  do_execsql_test printf2-3.2 {
000107    SELECT printf('|%-110.100c|','*');
000108  } {{|****************************************************************************************************          |}}
000109  do_execsql_test printf2-3.3 {
000110    SELECT printf('|%9.8c|%-9.8c|','*','*');
000111  } {{| ********|******** |}}
000112  do_execsql_test printf2-3.4 {
000113    SELECT printf('|%8.8c|%-8.8c|','*','*');
000114  } {|********|********|}
000115  do_execsql_test printf2-3.5 {
000116    SELECT printf('|%7.8c|%-7.8c|','*','*');
000117  } {|********|********|}
000118  
000119  # The "," separator
000120  do_execsql_test printf2-4.1 {
000121    SELECT printf('|%,d|%,d|',0,-1);
000122  } {|0|-1|}
000123  do_execsql_test printf2-4.2 {
000124    SELECT printf('|%,d|%,d|',12,-12);
000125  } {|12|-12|}
000126  do_execsql_test printf2-4.3 {
000127    SELECT printf('|%,d|%,d|',123,-123);
000128  } {|123|-123|}
000129  do_execsql_test printf2-4.4 {
000130    SELECT printf('|%,d|%,d|',1234,-1234);
000131  } {|1,234|-1,234|}
000132  do_execsql_test printf2-4.5 {
000133    SELECT printf('|%,d|%,d|',12345,-12345);
000134  } {|12,345|-12,345|}
000135  do_execsql_test printf2-4.6 {
000136    SELECT printf('|%,d|%,d|',123456,-123456);
000137  } {|123,456|-123,456|}
000138  do_execsql_test printf2-4.7 {
000139    SELECT printf('|%,d|%,d|',1234567,-1234567);
000140  } {|1,234,567|-1,234,567|}
000141  do_execsql_test printf2-4.8 {
000142    SELECT printf('|%,d|%,d|',12345678,-12345678);
000143  } {|12,345,678|-12,345,678|}
000144  do_execsql_test printf2-4.9 {
000145    SELECT printf('|%,d|%,d|',123456789,-123456789);
000146  } {|123,456,789|-123,456,789|}
000147  do_execsql_test printf2-4.10 {
000148    SELECT printf('|%,d|%,d|',1234567890,-1234567890);
000149  } {|1,234,567,890|-1,234,567,890|}
000150  
000151  
000152  
000153  finish_test