* Bug #14397 fixed - mgeti did not read u-int64 > 2^52
[scilab.git] / scilab / modules / fileio / tests / unit_tests / mgeti.tst
index f0e7079..0997231 100644 (file)
@@ -1,12 +1,42 @@
 // =============================================================================
 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 // Copyright (C) 2013 - Scilab Enterprises - Antoine ELIAS
+// Copyright (C) 2016 - Samuel GOUGEON
 //
 //  This file is distributed under the same license as the Scilab package.
 // =============================================================================
 
 // <-- CLI SHELL MODE -->
+// <-- NO CHECK REF -->
 
+// Read/Write int64 & uint64 with relative accuracy better than %eps = 1/2^52
+// --------------------------------------------------------------------------
+n = 10;
+b = grand(64,n,"uin",0,1);
+p = uint64(2).^ndgrid(0:63,1:n);
+x0 = sum(b.*p, "r");
+
+for endian = ["l" "b"]
+    for usign = ["u" ""]
+        // usign+"l"+endian
+        binfile = TMPDIR+"/mgetiTestInt64.dat";
+        idF = mopen(binfile, "w+b");
+        x = x0;
+        if usign==""
+            x = int64(x);
+        end
+        mput(x,usign+"l"+endian)
+
+        mseek(0);
+        xr = mgeti(n, usign+"l"+endian);
+        mclose(idF);
+        assert_checkequal(xr, x);
+    end
+end
+
+
+// Other tests
+// -----------
 function writedata(name, str)
     f = mopen(TMPDIR + "/" + name + ".dat", "wb");
     data = hex2dec(str);
@@ -23,13 +53,20 @@ endfunction
 //prepare data
 //
 
-ul1     = uint32(123); //0x7B
-ul2     = uint32(45678);//0xB26E
-ul3     = uint32(123456);//0x0001E240
-l1      = int32(-123); //0x85
-l2      = int32(-23456);//0xA460
-l3      = int32(-123456);//0xFFFE1DC0
-l3t     = int32(7616);//0x1DC0
+ul1     = uint64(123); //0x7B
+ul2     = uint64(45678);//0xB26E
+ul3     = uint64(123456);//0x0001E240
+l1      = int64(-123); //0x7B
+l2      = int64(-23456);//0xB26E
+l3      = int64(-123456);//0x0001E240
+
+ui1     = uint32(123); //0x7B
+ui2     = uint32(45678);//0xB26E
+ui3     = uint32(123456);//0x0001E240
+i1      = int32(-123); //0x85
+i2      = int32(-23456);//0xA460
+i3      = int32(-123456);//0xFFFE1DC0
+i3t     = int32(7616);//0x1DC0
 
 us1     = uint16(123); //0x7B
 us2     = uint16(45678);//0xB26E
@@ -40,18 +77,30 @@ s3      = int16(-123456);//0xFFFE1DC0
 s3t     = int16(7616);//0x1DC0
 
 //unsigned long big endian
-writedata("ulb", ["00" "00" "00" "7B" "00" "00" "B2" "6E" "00" "01" "E2" "40"]);
+writedata("ulb", ["00" "00" "00" "00" "00" "00" "00" "7B" "00" "00" "00" "00" "00" "00" "B2" "6E" "00" "00" "00" "00" "00" "01" "E2" "40"]);
 //unsigned long little endian
-writedata("ull", ["7B" "00" "00" "00" "6E" "B2" "00" "00" "40" "E2" "01" "00"]);
+writedata("ull", ["7B" "00" "00" "00" "00" "00" "00" "00" "6E" "B2" "00" "00" "00" "00" "00" "00" "40" "E2" "01" "00" "00" "00" "00" "00"]);
+
+//unsigned int big endian
+writedata("uib", ["00" "00" "00" "7B" "00" "00" "B2" "6E" "00" "01" "E2" "40"]);
+//unsigned int little endian
+writedata("uil", ["7B" "00" "00" "00" "6E" "B2" "00" "00" "40" "E2" "01" "00"]);
+
 //unsigned short big endian
 writedata("usb", ["00" "7B" "B2" "6E" "E2" "40"]);
 //unsigned short little endian
 writedata("usl", ["7B" "00" "6E" "B2" "40" "E2"]);
 
 //long big endian
-writedata("lb", ["FF" "FF" "FF" "85" "FF" "FF" "A4" "60" "FF" "FE" "1D" "C0"]);
+writedata("lb", ["FF" "FF" "FF" "FF" "FF" "FF" "FF" "85" "FF" "FF" "FF" "FF" "FF" "FF" "A4" "60" "FF" "FF" "FF" "FF" "FF" "FE" "1D" "C0"]);
 //long little endian
-writedata("ll", ["85" "FF" "FF" "FF" "60" "A4" "FF" "FF" "C0" "1D" "FE" "FF"]);
+writedata("ll", ["85" "FF" "FF" "FF" "FF" "FF" "FF" "FF" "60" "A4" "FF" "FF" "FF" "FF" "FF" "FF" "C0" "1D" "FE" "FF" "FF" "FF" "FF" "FF"]);
+
+//int big endian
+writedata("ib", ["FF" "FF" "FF" "85" "FF" "FF" "A4" "60" "FF" "FE" "1D" "C0"]);
+//int little endian
+writedata("il", ["85" "FF" "FF" "FF" "60" "A4" "FF" "FF" "C0" "1D" "FE" "FF"]);
+
 //short big endian
 writedata("sb", ["FF" "85" "A4" "60" "1D" "C0"]);
 //short little endian
@@ -61,16 +110,37 @@ res = readdata("ulb");
 assert_checkequal(res, [ul1, ul2, ul3]);
 res = readdata("ull");
 assert_checkequal(res, [ul1, ul2, ul3]);
+res = readdata("lb");
+assert_checkequal(res, [l1, l2, l3]);
+res = readdata("ll");
+assert_checkequal(res, [l1, l2, l3]);
+
+res = readdata("uib");
+assert_checkequal(res, [ui1, ui2, ui3]);
+res = readdata("uil");
+assert_checkequal(res, [ui1, ui2, ui3]);
 res = readdata("usb");
 assert_checkequal(res, [us1, us2, us3]);
 res = readdata("usl");
 assert_checkequal(res, [us1, us2, us3]);
 
-res = readdata("lb");
-assert_checkequal(res, [l1, l2, l3]);
-res = readdata("ll");
-assert_checkequal(res, [l1, l2, l3]);
+res = readdata("ib");
+assert_checkequal(res, [i1, i2, i3]);
+res = readdata("il");
+assert_checkequal(res, [i1, i2, i3]);
 res = readdata("sb");
 assert_checkequal(res, [s1, s2, s3]);
 res = readdata("sl");
 assert_checkequal(res, [s1, s2, s3]);
+
+// check default output type of mgeti
+file1 = fullfile(TMPDIR,'test1.bin');
+fd1=mopen(file1,'wb');
+mput(1996,'ull',fd1);
+mclose(fd1);
+
+fd1=mopen(file1,'rb');
+a=mgeti(1);
+assert_checkequal(typeof(a), "int32");
+assert_checkequal(a, int32(1996));
+mclose(fd1);