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-32560-14372 The format(FORMAT,...) SQL function works
000016  # like the sqlite3_mprintf() C-language function and the printf()
000017  # function from the standard C library.
000018  #
000019  # EVIDENCE-OF: R-64900-53159 The printf() SQL function is an alias for
000020  # the format() SQL function.
000021  #
000022  
000023  set testdir [file dirname $argv0]
000024  source $testdir/tester.tcl
000025  
000026  # EVIDENCE-OF: R-40086-60101 If the FORMAT argument is missing or NULL
000027  # then the result is NULL.
000028  #
000029  do_execsql_test printf2-1.1 {
000030    SELECT quote(format()), quote(format(NULL,1,2,3));
000031  } {NULL NULL}
000032  
000033  
000034  do_execsql_test printf2-1.2 {
000035    SELECT printf('hello');
000036  } {hello}
000037  do_execsql_test printf2-1.3 {
000038    SELECT format('%d,%d,%d',55,-11,3421);
000039  } {55,-11,3421}
000040  do_execsql_test printf2-1.4 {
000041    SELECT printf('%d,%d,%d',55,'-11',3421);
000042  } {55,-11,3421}
000043  do_execsql_test printf2-1.5 {
000044    SELECT format('%d,%d,%d,%d',55,'-11',3421);
000045  } {55,-11,3421,0}
000046  do_execsql_test printf2-1.6 {
000047    SELECT printf('%.2f',3.141592653);
000048  } {3.14}
000049  do_execsql_test printf2-1.7 {
000050    SELECT format('%.*f',2,3.141592653);
000051  } {3.14}
000052  do_execsql_test printf2-1.8 {
000053    SELECT printf('%*.*f',5,2,3.141592653);
000054  } {{ 3.14}}
000055  do_execsql_test printf2-1.9 {
000056    SELECT format('%d',314159.2653);
000057  } {314159}
000058  do_execsql_test printf2-1.10 {
000059    SELECT printf('%lld',314159.2653);
000060  } {314159}
000061  do_execsql_test printf2-1.11 {
000062    SELECT format('%lld%n',314159.2653,'hi');
000063  } {314159}
000064  do_execsql_test printf2-1.12 {
000065    SELECT printf('%n',0);
000066  } {{}}
000067  
000068  # EVIDENCE-OF: R-17002-27534 The %z format is interchangeable with %s.
000069  #
000070  do_execsql_test printf2-1.12 {
000071    SELECT format('%.*z',5,'abcdefghijklmnop');
000072  } {abcde}
000073  do_execsql_test printf2-1.13 {
000074    SELECT printf('%c','abcdefghijklmnop');
000075  } {a}
000076  
000077  # EVIDENCE-OF: R-02347-27622 The %n format is silently ignored and does
000078  # not consume an argument.
000079  #
000080  do_execsql_test printf2-2.1 {
000081    CREATE TABLE t1(a,b,c);
000082    INSERT INTO t1 VALUES(1,2,3);
000083    INSERT INTO t1 VALUES(-1,-2,-3);
000084    INSERT INTO t1 VALUES('abc','def','ghi');
000085    INSERT INTO t1 VALUES(1.5,2.25,3.125);
000086    SELECT printf('(%s)-%n-(%s)',a,b,c) FROM t1 ORDER BY rowid;
000087  } {(1)--(2) (-1)--(-2) (abc)--(def) (1.5)--(2.25)}
000088  
000089  # EVIDENCE-OF: R-56064-04001 The %p format is an alias for %X.
000090  #
000091  do_execsql_test printf2-2.2 {
000092    SELECT printf('%s=(%p)',a,a) FROM t1 ORDER BY a;
000093  } {-1=(FFFFFFFFFFFFFFFF) 1=(1) 1.5=(1) abc=(0)}
000094  
000095  # EVIDENCE-OF: R-29410-53018 If there are too few arguments in the
000096  # argument list, missing arguments are assumed to have a NULL value,
000097  # which is translated into 0 or 0.0 for numeric formats or an empty
000098  # string for %s.
000099  #
000100  do_execsql_test printf2-2.3 {
000101    SELECT printf('%s=(%d/%g/%s)',a) FROM t1 ORDER BY a;
000102  } {-1=(0/0/) 1=(0/0/) 1.5=(0/0/) abc=(0/0/)}
000103  
000104  # The precision of the %c conversion causes the character to repeat.
000105  #
000106  do_execsql_test printf2-3.1 {
000107    SELECT printf('|%110.100c|','*');
000108  } {{|          ****************************************************************************************************|}}
000109  do_execsql_test printf2-3.2 {
000110    SELECT printf('|%-110.100c|','*');
000111  } {{|****************************************************************************************************          |}}
000112  do_execsql_test printf2-3.3 {
000113    SELECT printf('|%9.8c|%-9.8c|','*','*');
000114  } {{| ********|******** |}}
000115  do_execsql_test printf2-3.4 {
000116    SELECT printf('|%8.8c|%-8.8c|','*','*');
000117  } {|********|********|}
000118  do_execsql_test printf2-3.5 {
000119    SELECT printf('|%7.8c|%-7.8c|','*','*');
000120  } {|********|********|}
000121  
000122  # The "," separator
000123  do_execsql_test printf2-4.1 {
000124    SELECT printf('|%,d|%,d|',0,-1);
000125  } {|0|-1|}
000126  do_execsql_test printf2-4.2 {
000127    SELECT printf('|%,d|%,d|',12,-12);
000128  } {|12|-12|}
000129  do_execsql_test printf2-4.3 {
000130    SELECT printf('|%,d|%,d|',123,-123);
000131  } {|123|-123|}
000132  do_execsql_test printf2-4.4 {
000133    SELECT printf('|%,d|%,d|',1234,-1234);
000134  } {|1,234|-1,234|}
000135  do_execsql_test printf2-4.5 {
000136    SELECT printf('|%,d|%,d|',12345,-12345);
000137  } {|12,345|-12,345|}
000138  do_execsql_test printf2-4.6 {
000139    SELECT printf('|%,d|%,d|',123456,-123456);
000140  } {|123,456|-123,456|}
000141  do_execsql_test printf2-4.7 {
000142    SELECT printf('|%,d|%,d|',1234567,-1234567);
000143  } {|1,234,567|-1,234,567|}
000144  do_execsql_test printf2-4.8 {
000145    SELECT printf('|%,d|%,d|',12345678,-12345678);
000146  } {|12,345,678|-12,345,678|}
000147  do_execsql_test printf2-4.9 {
000148    SELECT printf('|%,d|%,d|',123456789,-123456789);
000149  } {|123,456,789|-123,456,789|}
000150  do_execsql_test printf2-4.10 {
000151    SELECT printf('|%,d|%,d|',1234567890,-1234567890);
000152  } {|1,234,567,890|-1,234,567,890|}
000153  
000154  # 2018-02-19.  Unicode characters with %c
000155  do_execsql_test printf2-5.100 {
000156    SELECT printf('(%8c)',char(11106));
000157  } {{(       ⭢)}}
000158  do_execsql_test printf2-5.101 {
000159    SELECT printf('(%-8c)',char(11106));
000160  } {{(⭢       )}}
000161  do_execsql_test printf2-5.102 {
000162    SELECT printf('(%5.3c)',char(1492));
000163  } {{(  ההה)}}
000164  do_execsql_test printf2-5.103 {
000165    SELECT printf('(%-5.3c)',char(1492));
000166  } {{(ההה  )}}
000167  do_execsql_test printf2-5.104 {
000168    SELECT printf('(%3.3c)',char(1492));
000169  } {{(ההה)}}
000170  do_execsql_test printf2-5.105 {
000171    SELECT printf('(%-3.3c)',char(1492));
000172  } {{(ההה)}}
000173  do_execsql_test printf2-5.104 {
000174    SELECT printf('(%2c)',char(1513));
000175  } {{( ש)}}
000176  do_execsql_test printf2-5.106 {
000177    SELECT printf('(%-2c)',char(1513));
000178  } {{(ש )}}
000179  
000180  # 2018-02-19.  Unicode characters with the "!" flag in %s and friends.
000181  do_execsql_test printf2-6.100 {
000182    SELECT printf('(%!.3s)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד');
000183  } {(הנה)}
000184  do_execsql_test printf2-6.101 {
000185    SELECT printf('(%.6s)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד');
000186  } {(הנה)}
000187  do_execsql_test printf2-6.102 {
000188    SELECT printf('(%!5.3s)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד');
000189  } {{(  הנה)}}
000190  do_execsql_test printf2-6.103 {
000191    SELECT printf('(%8.6s)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד');
000192  } {{(  הנה)}}
000193  do_execsql_test printf2-6.104 {
000194    SELECT printf('(%!-5.3s)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד');
000195  } {{(הנה  )}}
000196  do_execsql_test printf2-6.105 {
000197    SELECT printf('(%-8.6s)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד');
000198  } {{(הנה  )}}
000199  do_execsql_test printf2-6.106 {
000200    SELECT printf('(%!.3Q)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד');
000201  } {('הנה')}
000202  do_execsql_test printf2-6.107 {
000203    SELECT printf('(%.6Q)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד');
000204  } {('הנה')}
000205  do_execsql_test printf2-6.108 {
000206    SELECT printf('(%!7.3Q)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד');
000207  } {{(  'הנה')}}
000208  do_execsql_test printf2-6.109 {
000209    SELECT printf('(%10.6Q)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד');
000210  } {{(  'הנה')}}
000211  
000212  
000213  finish_test