summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorTom Lane2024-09-26 15:02:31 +0000
committerTom Lane2024-09-26 15:02:31 +0000
commit147bbc90f75794e5522dcbadaf2bbe1af3ce574a (patch)
treee84f92a11a5b766109dc51fe062d56e87cf41030 /src/test
parente3a92ab0708aa8ac0c8466312cef316ea6d03c63 (diff)
Modernize to_char's Roman-numeral code, fixing overflow problems.
int_to_roman() only accepts plain "int" input, which is fine since we're going to produce '###############' for any value above 3999 anyway. However, the numeric and int8 variants of to_char() would throw an error if the given input exceeded the integer range, while the float-input variants invoked undefined-per-C-standard behavior. Fix things so that you uniformly get '###############' for out of range input. Also add test cases covering this code, plus the equally-untested EEEE, V, and PL format codes. Discussion: https://postgr.es/m/2956175.1725831136@sss.pgh.pa.us
Diffstat (limited to 'src/test')
-rw-r--r--src/test/regress/expected/int8.out50
-rw-r--r--src/test/regress/expected/numeric.out87
-rw-r--r--src/test/regress/sql/int8.sql8
-rw-r--r--src/test/regress/sql/numeric.sql15
4 files changed, 160 insertions, 0 deletions
diff --git a/src/test/regress/expected/int8.out b/src/test/regress/expected/int8.out
index fddc09f6305..392f3bf655c 100644
--- a/src/test/regress/expected/int8.out
+++ b/src/test/regress/expected/int8.out
@@ -530,6 +530,16 @@ SELECT to_char(q2, 'MI9999999999999999') FROM INT8_TBL;
-4567890123456789
(5 rows)
+SELECT to_char(q2, '9999999999999999PL') FROM INT8_TBL;
+ to_char
+--------------------
+ 456+
+ 4567890123456789+
+ 123+
+ 4567890123456789+
+ -4567890123456789
+(5 rows)
+
SELECT to_char(q2, 'FMS9999999999999999') FROM INT8_TBL;
to_char
-------------------
@@ -650,6 +660,46 @@ SELECT to_char(q2, '999999SG9999999999') FROM INT8_TBL;
456789-0123456789
(5 rows)
+SELECT to_char(q2, 'FMRN') FROM INT8_TBL;
+ to_char
+-----------------
+ CDLVI
+ ###############
+ CXXIII
+ ###############
+ ###############
+(5 rows)
+
+SELECT to_char(1234, '9.99EEEE');
+ to_char
+-----------
+ 1.23e+03
+(1 row)
+
+SELECT to_char(1234::int8, '9.99eeee');
+ to_char
+-----------
+ 1.23e+03
+(1 row)
+
+SELECT to_char(-1234::int8, '9.99eeee');
+ to_char
+-----------
+ -1.23e+03
+(1 row)
+
+SELECT to_char(1234, '99999V99');
+ to_char
+----------
+ 123400
+(1 row)
+
+SELECT to_char(1234::int8, '99999V99');
+ to_char
+----------
+ 123400
+(1 row)
+
-- check min/max values and overflow behavior
select '-9223372036854775808'::int8;
int8
diff --git a/src/test/regress/expected/numeric.out b/src/test/regress/expected/numeric.out
index f30ac236f52..d54282496ab 100644
--- a/src/test/regress/expected/numeric.out
+++ b/src/test/regress/expected/numeric.out
@@ -1991,6 +1991,21 @@ SELECT to_char(val, '9.999EEEE') FROM num_data;
-2.493e+07
(10 rows)
+SELECT to_char(val, 'FMRN') FROM num_data;
+ to_char
+-----------------
+ ###############
+ ###############
+ ###############
+ IV
+ ###############
+ ###############
+ ###############
+ ###############
+ ###############
+ ###############
+(10 rows)
+
WITH v(val) AS
(VALUES('0'::numeric),('-4.2'),('4.2e9'),('1.2e-5'),('inf'),('-inf'),('nan'))
SELECT val,
@@ -2101,6 +2116,72 @@ SELECT to_char('12345678901'::float8, 'FM9999999999D9999900000000000000000');
##########.####
(1 row)
+SELECT to_char('100'::numeric, 'rn');
+ to_char
+-----------------
+ c
+(1 row)
+
+SELECT to_char('1234'::numeric, 'rn');
+ to_char
+-----------------
+ mccxxxiv
+(1 row)
+
+SELECT to_char('1235'::float4, 'rn');
+ to_char
+-----------------
+ mccxxxv
+(1 row)
+
+SELECT to_char('1236'::float8, 'rn');
+ to_char
+-----------------
+ mccxxxvi
+(1 row)
+
+SELECT to_char('1237'::float8, 'fmrn');
+ to_char
+-----------
+ mccxxxvii
+(1 row)
+
+SELECT to_char('100e9'::numeric, 'RN');
+ to_char
+-----------------
+ ###############
+(1 row)
+
+SELECT to_char('100e9'::float4, 'RN');
+ to_char
+-----------------
+ ###############
+(1 row)
+
+SELECT to_char('100e9'::float8, 'RN');
+ to_char
+-----------------
+ ###############
+(1 row)
+
+SELECT to_char(1234.56::numeric, '99999V99');
+ to_char
+----------
+ 123456
+(1 row)
+
+SELECT to_char(1234.56::float4, '99999V99');
+ to_char
+----------
+ 123456
+(1 row)
+
+SELECT to_char(1234.56::float8, '99999V99');
+ to_char
+----------
+ 123456
+(1 row)
+
-- Check parsing of literal text in a format string
SELECT to_char('100'::numeric, 'foo999');
to_char
@@ -2297,6 +2378,12 @@ SELECT to_number('42nd', '99th');
42
(1 row)
+SELECT to_number('123456', '99999V99');
+ to_number
+-------------------------
+ 1234.560000000000000000
+(1 row)
+
RESET lc_numeric;
--
-- Input syntax
diff --git a/src/test/regress/sql/int8.sql b/src/test/regress/sql/int8.sql
index fffb28906a1..53359f06ade 100644
--- a/src/test/regress/sql/int8.sql
+++ b/src/test/regress/sql/int8.sql
@@ -110,6 +110,7 @@ SELECT to_char( (q1 * -1), '9999999999999999S'), to_char( (q2 * -1), 'S999999999
FROM INT8_TBL;
SELECT to_char(q2, 'MI9999999999999999') FROM INT8_TBL;
+SELECT to_char(q2, '9999999999999999PL') FROM INT8_TBL;
SELECT to_char(q2, 'FMS9999999999999999') FROM INT8_TBL;
SELECT to_char(q2, 'FM9999999999999999THPR') FROM INT8_TBL;
SELECT to_char(q2, 'SG9999999999999999th') FROM INT8_TBL;
@@ -122,6 +123,13 @@ SELECT to_char(q2, 'FM9999999999999999.999') FROM INT8_TBL;
SELECT to_char(q2, 'S 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 . 9 9 9') FROM INT8_TBL;
SELECT to_char(q2, E'99999 "text" 9999 "9999" 999 "\\"text between quote marks\\"" 9999') FROM INT8_TBL;
SELECT to_char(q2, '999999SG9999999999') FROM INT8_TBL;
+SELECT to_char(q2, 'FMRN') FROM INT8_TBL;
+
+SELECT to_char(1234, '9.99EEEE');
+SELECT to_char(1234::int8, '9.99eeee');
+SELECT to_char(-1234::int8, '9.99eeee');
+SELECT to_char(1234, '99999V99');
+SELECT to_char(1234::int8, '99999V99');
-- check min/max values and overflow behavior
diff --git a/src/test/regress/sql/numeric.sql b/src/test/regress/sql/numeric.sql
index c86395209ab..b508cba71dd 100644
--- a/src/test/regress/sql/numeric.sql
+++ b/src/test/regress/sql/numeric.sql
@@ -996,6 +996,7 @@ SELECT to_char(val, E'99999 "text" 9999 "9999" 999 "\\"text between quote marks\
SELECT to_char(val, '999999SG9999999999') FROM num_data;
SELECT to_char(val, 'FM9999999999999999.999999999999999') FROM num_data;
SELECT to_char(val, '9.999EEEE') FROM num_data;
+SELECT to_char(val, 'FMRN') FROM num_data;
WITH v(val) AS
(VALUES('0'::numeric),('-4.2'),('4.2e9'),('1.2e-5'),('inf'),('-inf'),('nan'))
@@ -1033,6 +1034,19 @@ SELECT to_char('100'::numeric, 'FM999.');
SELECT to_char('100'::numeric, 'FM999');
SELECT to_char('12345678901'::float8, 'FM9999999999D9999900000000000000000');
+SELECT to_char('100'::numeric, 'rn');
+SELECT to_char('1234'::numeric, 'rn');
+SELECT to_char('1235'::float4, 'rn');
+SELECT to_char('1236'::float8, 'rn');
+SELECT to_char('1237'::float8, 'fmrn');
+SELECT to_char('100e9'::numeric, 'RN');
+SELECT to_char('100e9'::float4, 'RN');
+SELECT to_char('100e9'::float8, 'RN');
+
+SELECT to_char(1234.56::numeric, '99999V99');
+SELECT to_char(1234.56::float4, '99999V99');
+SELECT to_char(1234.56::float8, '99999V99');
+
-- Check parsing of literal text in a format string
SELECT to_char('100'::numeric, 'foo999');
SELECT to_char('100'::numeric, 'f\oo999');
@@ -1070,6 +1084,7 @@ SELECT to_number('$1,234.56','L99,999.99');
SELECT to_number('1234.56','L99,999.99');
SELECT to_number('1,234.56','L99,999.99');
SELECT to_number('42nd', '99th');
+SELECT to_number('123456', '99999V99');
RESET lc_numeric;
--