2da1e0
From b6e3965aa874cd3391eb01b106af3aa2716f1b1b Mon Sep 17 00:00:00 2001
2da1e0
From: Vladimir Marek <vlmarek@volny.cz>
2da1e0
Date: Wed, 2 Mar 2016 11:14:06 +0100
2da1e0
Subject: [PATCH] Tests for little-endian platform
2da1e0
MIME-Version: 1.0
2da1e0
Content-Type: text/plain; charset=UTF-8
2da1e0
Content-Transfer-Encoding: 8bit
2da1e0
2da1e0
Ported to 4.023:
2da1e0
2da1e0
commit 55363b41ea077778b723ee5600316141e2785541
2da1e0
Author: Vladimir Marek <vlmarek@volny.cz>
2da1e0
Date:   Wed Mar 2 11:14:06 2016 +0100
2da1e0
2da1e0
    Tests for little-endian platform
2da1e0
2da1e0
    https://rt.cpan.org/Public/Bug/Display.html?id=57266
2da1e0
2da1e0
Signed-off-by: Petr Písař <ppisar@redhat.com>
2da1e0
---
2da1e0
 t/41int_min_max.t | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
2da1e0
 1 file changed, 149 insertions(+)
2da1e0
 create mode 100644 t/41int_min_max.t
2da1e0
2da1e0
diff --git a/t/41int_min_max.t b/t/41int_min_max.t
2da1e0
new file mode 100644
2da1e0
index 0000000..fbb622c
2da1e0
--- /dev/null
2da1e0
+++ b/t/41int_min_max.t
2da1e0
@@ -0,0 +1,149 @@
2da1e0
+use strict;
2da1e0
+use warnings;
2da1e0
+use bigint;
2da1e0
+
2da1e0
+use DBI;
2da1e0
+use DBI::Const::GetInfoType;
2da1e0
+use Test::More;
2da1e0
+use lib 't', '.';
2da1e0
+use Data::Dumper;
2da1e0
+require 'lib.pl';
2da1e0
+use vars qw($test_dsn $test_user $test_password);
2da1e0
+
2da1e0
+my $dbh;
2da1e0
+eval {$dbh= DBI->connect($test_dsn, $test_user, $test_password,
2da1e0
+                      { RaiseError => 1, PrintError => 1, AutoCommit => 1 });};
2da1e0
+if ($@) {
2da1e0
+    plan skip_all => "no database connection";
2da1e0
+}
2da1e0
+
2da1e0
+if ($dbh->get_info($GetInfoType{SQL_DBMS_VER}) lt "4.1") {
2da1e0
+    plan skip_all => "ERROR: $DBI::errstr. Can't continue test";
2da1e0
+    plan skip_all =>
2da1e0
+        "SKIP TEST: You must have MySQL version 4.1 and greater for this test to run";
2da1e0
+}
2da1e0
+# nostrict tests + strict tests + init/tear down commands
2da1e0
+plan tests => 19*8 + 17*8 + 4;
2da1e0
+
2da1e0
+my $table = 'dbd_mysql_t41minmax'; # name of the table we will be using
2da1e0
+my $mode; # 'strict' or 'nostrict' corresponds to strict SQL mode
2da1e0
+
2da1e0
+sub test_int_type ($$$$) {
2da1e0
+    my ($perl_type, $mysql_type, $min, $max) = @_;
2da1e0
+
2da1e0
+    # Disable the warning text clobbering our output
2da1e0
+    local $SIG{__WARN__} = sub { 1; };
2da1e0
+
2da1e0
+    # Create the table
2da1e0
+    ok($dbh->do(qq{DROP TABLE IF EXISTS $table}), "removing $table");
2da1e0
+    ok($dbh->do(qq{
2da1e0
+            CREATE TABLE `$table` (
2da1e0
+                `id` int not null auto_increment,
2da1e0
+                `val` $mysql_type,
2da1e0
+                primary key (id)
2da1e0
+            )
2da1e0
+    }), "creating minmax table for type $mysql_type");
2da1e0
+
2da1e0
+    my ($store, $retrieve); # statements
2da1e0
+    my $read_value;         # retrieved value
2da1e0
+    ok($store = $dbh->prepare("INSERT INTO $table (val) VALUES (?)"));
2da1e0
+    ok($retrieve = $dbh->prepare("SELECT val from $table where id=(SELECT MAX(id) FROM $table)"));
2da1e0
+
2da1e0
+    ########################################
2da1e0
+    # Insert allowed min value
2da1e0
+    ########################################
2da1e0
+    ok($store->bind_param( 1, $min->bstr(), $perl_type ), "binding minimal $mysql_type, mode=$mode");
2da1e0
+    ok($store->execute(), "inserting min data for type $mysql_type, mode=$mode");
2da1e0
+
2da1e0
+    ########################################
2da1e0
+    # Read it back and compare
2da1e0
+    ########################################
2da1e0
+    ok{$retrieve->execute()};
2da1e0
+    ($read_value) = $retrieve->fetchrow_array();
2da1e0
+    cmp_ok($read_value, 'eq', $min, "retrieved minimal value for $mysql_type, mode=$mode");
2da1e0
+
2da1e0
+    ########################################
2da1e0
+    # Insert allowed max value
2da1e0
+    ########################################
2da1e0
+    ok($store->bind_param( 1, $max->bstr(), $perl_type ), "binding maximal $mysql_type, mode=$mode");
2da1e0
+    ok($store->execute(), "inserting max data for type $mysql_type, mode=$mode");
2da1e0
+
2da1e0
+    ########################################
2da1e0
+    # Read it back and compare
2da1e0
+    ########################################
2da1e0
+    ok{$retrieve->execute()};
2da1e0
+    ($read_value) = $retrieve->fetchrow_array();
2da1e0
+    cmp_ok($read_value, 'eq', $max, "retrieved maximal value for $mysql_type, mode=$mode");
2da1e0
+
2da1e0
+    ########################################
2da1e0
+    # Try to insert under the limit value
2da1e0
+    ########################################
2da1e0
+    ok($store->bind_param( 1, ($min-1)->bstr(), $perl_type ), "binding less than minimal $mysql_type, mode=$mode");
2da1e0
+    if ($mode eq 'strict') {
2da1e0
+        $@ = '';
2da1e0
+        eval{$store->execute()};
2da1e0
+        like($@, qr/Out of range value for column 'val'/, "Error, you stored ".($min-1)." into $mysql_type, mode=$mode\n".
2da1e0
+            Data::Dumper->Dump([$dbh->selectall_arrayref("SELECT * FROM $table")]).
2da1e0
+            Data::Dumper->Dump([$dbh->selectall_arrayref("describe $table")])
2da1e0
+        );
2da1e0
+    } else {
2da1e0
+        ok{$store->execute()};
2da1e0
+        ########################################
2da1e0
+        # Check that it was rounded correctly
2da1e0
+        ########################################
2da1e0
+        ok{$retrieve->execute()};
2da1e0
+        ($read_value) = $retrieve->fetchrow_array();
2da1e0
+        cmp_ok($read_value, 'eq', $min, "retrieved minimal value for type $mysql_type, mode=$mode");
2da1e0
+    };
2da1e0
+
2da1e0
+    ########################################
2da1e0
+    # Try to insert over the limit value
2da1e0
+    ########################################
2da1e0
+    ok($store->bind_param( 1, ($max+1)->bstr(), $perl_type ), "binding more than maximal $mysql_type, mode=$mode");
2da1e0
+    if ($mode eq 'strict') {
2da1e0
+        $@ = '';
2da1e0
+        eval{$store->execute()};
2da1e0
+        like($@, qr/Out of range value for column 'val'/, "Error, you stored ".($max+1)." into $mysql_type, mode=$mode\n".
2da1e0
+            Data::Dumper->Dump([$dbh->selectall_arrayref("SELECT * FROM $table")]).
2da1e0
+            Data::Dumper->Dump([$dbh->selectall_arrayref("describe $table")])
2da1e0
+        );
2da1e0
+    } else {
2da1e0
+        ok{$store->execute()};
2da1e0
+        ########################################
2da1e0
+        # Check that it was rounded correctly
2da1e0
+        ########################################
2da1e0
+        ok{$retrieve->execute()};
2da1e0
+        ($read_value) = $retrieve->fetchrow_array();
2da1e0
+        cmp_ok($read_value, 'eq', $max, "retrieved maximal value for type $mysql_type, mode=$mode");
2da1e0
+    };
2da1e0
+}
2da1e0
+
2da1e0
+# Set strict SQL mode
2da1e0
+ok($dbh->do("SET SQL_MODE='STRICT_ALL_TABLES'"),"Enter strict SQL mode.");
2da1e0
+$mode = 'strict';
2da1e0
+
2da1e0
+test_int_type(DBI::SQL_TINYINT,  'tinyint signed',     -2**7,  2**7-1);
2da1e0
+test_int_type(DBI::SQL_TINYINT,  'tinyint unsigned',       0,  2**8-1);
2da1e0
+test_int_type(DBI::SQL_SMALLINT, 'smallint signed',   -2**15, 2**15-1);
2da1e0
+test_int_type(DBI::SQL_SMALLINT, 'smallint unsigned',      0, 2**16-1);
2da1e0
+test_int_type(DBI::SQL_INTEGER,  'int signed',        -2**31, 2**31-1);
2da1e0
+test_int_type(DBI::SQL_INTEGER,  'int unsigned',           0, 2**32-1);
2da1e0
+test_int_type(DBI::SQL_BIGINT,   'bigint signed',     -2**63, 2**63-1);
2da1e0
+test_int_type(DBI::SQL_BIGINT,   'bigint unsigned',        0, 2**64-1);
2da1e0
+
2da1e0
+# Do not use strict SQL mode
2da1e0
+ok($dbh->do("SET SQL_MODE=''"),"Leave strict SQL mode.");
2da1e0
+$mode = 'nostrict';
2da1e0
+
2da1e0
+test_int_type(DBI::SQL_TINYINT,  'tinyint signed',     -2**7,  2**7-1);
2da1e0
+test_int_type(DBI::SQL_TINYINT,  'tinyint unsigned',       0,  2**8-1);
2da1e0
+test_int_type(DBI::SQL_SMALLINT, 'smallint signed',   -2**15, 2**15-1);
2da1e0
+test_int_type(DBI::SQL_SMALLINT, 'smallint unsigned',      0, 2**16-1);
2da1e0
+test_int_type(DBI::SQL_INTEGER,  'int signed',        -2**31, 2**31-1);
2da1e0
+test_int_type(DBI::SQL_INTEGER,  'int unsigned',           0, 2**32-1);
2da1e0
+test_int_type(DBI::SQL_BIGINT,   'bigint signed',     -2**63, 2**63-1);
2da1e0
+test_int_type(DBI::SQL_BIGINT,   'bigint unsigned',        0, 2**64-1);
2da1e0
+
2da1e0
+ok ($dbh->do("DROP TABLE $table"));
2da1e0
+
2da1e0
+ok $dbh->disconnect;
2da1e0
-- 
2da1e0
2.7.4
2da1e0