Search notes:

Test for function SPRINTF of PL/SQL package TXT

This is a test case for the function sprintf of the PL/SQL package txt.
set serveroutput on size 100000 format wrapped

declare

   procedure cmp(test_no in number, format in varchar2, parms in varchar2_t, expected in varchar2) is -- {
     gotten varchar2(4000);
   begin

     gotten := txt.sprintf(format, parms);

     if gotten != expected then

        dbms_output.put_line('test ' || test_no || ' failed');
        dbms_output.put_line('    gotten is: ' || gotten    || '<');
        dbms_output.put_line('  expected is: ' || expected  || '<');

     end if;

   end cmp; -- }

begin

-- Right aligning a string:
   cmp( 1,  'String: %20s', varchar2_t('hello world'), 'String:          hello world');

-- Left aligning a string                                    12345678901234567890
   cmp( 2, 'String: %-20s', varchar2_t('hello world'), 'String: hello world         ');

-- A left and a right aligned string                         ... 1234567890 1234567890 ...
   cmp( 3, '... %10s %-10s ...', varchar2_t('hello', 'world'), '...      hello world      ...');

-- Strings and null                                                     ... 12345 12345 12345 12345 ...
   cmp( 4, '... %5s %5s %-5s %-5s ...', varchar2_t('a', null, 'b', null), '...     a       b           ...');

-- Numbers
   cmp( 5, 'Numbers: %d,%d,%d'  , varchar2_t(  42,    0, -42), 'Numbers: 42,0,-42');
   cmp( 6, 'Numbers: %d,%d,%d'  , varchar2_t(  42, null, -42), 'Numbers: 42,,-42' );

-- Right aligning numbers:                                               12345 12345 12345
   cmp( 7, 'Numbers: %5d,%5d,%5d'  , varchar2_t(  42,    0, -42), 'Numbers:    42,    0,  -42');
   cmp( 8, 'Numbers: %5d,%5d,%5d'  , varchar2_t(  42, null, -42), 'Numbers:    42,     ,  -42');

-- Left aligning numbers:                                                   12345 12345 12345
   cmp( 9, 'Numbers: %-5d,%-5d,%-5d'  , varchar2_t(  42, null, -42), 'Numbers: 42   ,     ,-42  ');

-- Fractions                                   12.12345
   cmp(10, '1/3: %2.5d',  varchar2_t( 1/3), '1/3:   .33333');
   cmp(11, '1/3: %02.5d', varchar2_t( 1/3), '1/3:  0.33333');
   cmp(12, '1/3: %2.5d',  varchar2_t(-1/3), '1/3:  -.33333');
   cmp(13, '1/3: %02.5d', varchar2_t(-1/3), '1/3: -0.33333');

-- Fractions with signs                         S12.12345
   cmp(14, '1/3: %+2.5d',  varchar2_t( 1/3), '1/3:   +.33333');
   cmp(15, '1/3: %+02.5d', varchar2_t( 1/3), '1/3:  +0.33333');
   cmp(16, '1/3: %+2.5d',  varchar2_t(-1/3), '1/3:   -.33333');
   cmp(17, '1/3: %+02.5d', varchar2_t(-1/3), '1/3:  -0.33333');

-- Recognizition of the %
   cmp(18, '%d %% of %d is: %d', varchar2_t(7, 68, 68/100*7), '7 % of 68 is: 4.76');

-- Number doesn't fit the length
   cmp(19, '... %4d ...', varchar2_t(12345), '... #### ...');

end;
/

Github repository PL-SQL-pkg-txt, path: /_test/sprintf.sql

Index