* Bug #12360 fixed - Incorrect format for purely real complexs in csvWrite. 73/10773/3
Simon Marchetto [Tue, 12 Mar 2013 10:54:39 +0000 (11:54 +0100)]
Fix: do not display imaginary part on purely real complexs,
    & real part on purely imaginary complexs

+ add unit test complex data

Change-Id: I38ba3d83cba3b884350bd07ceb3bb33281498cde

scilab/CHANGES_5.4.X
scilab/modules/spreadsheet/src/c/csvWrite.c
scilab/modules/spreadsheet/tests/nonreg_tests/bug_12360.dia.ref [new file with mode: 0644]
scilab/modules/spreadsheet/tests/nonreg_tests/bug_12360.ref.csv [new file with mode: 0644]
scilab/modules/spreadsheet/tests/nonreg_tests/bug_12360.tst [new file with mode: 0644]
scilab/modules/spreadsheet/tests/nonreg_tests/bug_8349.dia.ref
scilab/modules/spreadsheet/tests/nonreg_tests/bug_8349.tst
scilab/modules/spreadsheet/tests/unit_tests/complex.csv [new file with mode: 0644]
scilab/modules/spreadsheet/tests/unit_tests/csvWrite.dia.ref
scilab/modules/spreadsheet/tests/unit_tests/csvWrite.tst

index fba52cb..6ab9e03 100644 (file)
@@ -178,7 +178,7 @@ Xcos
 * Bug #12351 fixed - "Save as" a diagram to a selected xcos file type, saved
                      it as zcos.
 
-* Bug #12352 fixed - Critical error when closing a scope windows during 
+* Bug #12352 fixed - Critical error when closing a scope windows during
                      simulation fixed.
 
 
@@ -515,6 +515,8 @@ Bug fixes
 * Bug #12354 fixed - In SciNotes, invalid function with a semicolon after args
                      declaration.
 
+* Bug #12360 fixed - Incorrect format for purely real complexs in csvWrite.
+
 * Bug #12361 fixed - Error fixed in replot() help page examples.
 
 * Bug #12373 fixed - pmodulo(n,m<0) returned a negative result.
index f11486a..809a4d6 100644 (file)
@@ -42,6 +42,9 @@
 #else
 #define MODEWFD "w"
 #endif
+#if _MSC_VER
+#define snprintf _snprintf
+#endif
 // =============================================================================
 #ifndef signbit
 static int signbit(double x)
@@ -296,88 +299,123 @@ csvWriteError csvWrite_complex(const char *filename,
         for (j = 0; j < n; j++)
         {
             char StringValue[65535];
-            if (ISNAN(pdValuesReal[i + m * j]))
+
+            double realValue = pdValuesReal[i + m * j];
+            double imagValue = pdValuesImag[i + m * j];
+            int hasReal = 0;
+
+            if (ISNAN(realValue))
             {
                 strcpy(StringValue, NanString);
+                hasReal = 1;
             }
-            else if (finite(pdValuesReal[i + m * j]))
+            else if (finite(realValue))
             {
-                char buffer[65535];
-                char *result = NULL;
-                sprintf(buffer, precisionFormat, pdValuesReal[i + m * j]);
-                result = csv_strsubst(buffer, getCsvDefaultDecimal(), decimal);
-                if (result)
+                if ((realValue != 0) || (finite(imagValue) && (imagValue == 0)))
                 {
-                    strcpy(StringValue, result);
-                    FREE(result);
-                    result = NULL;
-                }
-                else
-                {
-                    sprintf(StringValue, DEFAULT_CSV_WRITE_DOUBLE_FORMAT, pdValuesReal[i + m * j]);
+                    char buffer[65535];
+                    char *result = NULL;
+
+                    sprintf(buffer, precisionFormat, pdValuesReal[i + m * j]);
+                    result = csv_strsubst(buffer, getCsvDefaultDecimal(), decimal);
+                    if (result)
+                    {
+                        strcpy(StringValue, result);
+                        FREE(result);
+                    }
+                    else
+                    {
+                        sprintf(StringValue, DEFAULT_CSV_WRITE_DOUBLE_FORMAT, pdValuesReal[i + m * j]);
+                    }
+                    hasReal = 1;
                 }
             }
             else
             {
-                if ( signbit(pdValuesReal[i + m * j]) )
+                if (signbit(realValue))
                 {
-                    // NegInfString
                     strcpy(StringValue, NegInfString);
                 }
                 else
                 {
-                    // InfString
                     strcpy(StringValue, InfString);
                 }
+                hasReal = 1;
             }
 
-            if (ISNAN(pdValuesImag[i + m * j]))
-            {
-                strcat(StringValue, PlusStr);
-                strcat(StringValue, NanString);
-                strcat(StringValue, ComplexStr);
-            }
-            else if (finite(pdValuesImag[i + m * j]))
+            if (ISNAN(imagValue))
             {
-                char buffer[65535];
-                char *result = NULL;
-
-                if (pdValuesImag[i + m * j] >= 0)
+                if (hasReal)
                 {
                     strcat(StringValue, PlusStr);
+                    strcat(StringValue, NanString);
                 }
                 else
                 {
-                    strcat(StringValue, LessStr);
+                    strcpy(StringValue, NanString);
                 }
+                strcat(StringValue, ComplexStr);
+            }
+            else if (finite(imagValue))
+            {
+                if (imagValue != 0)
+                {
+                    char buffer[65535];
+                    char *result = NULL;
 
-                sprintf(buffer, precisionFormat, fabs(pdValuesImag[i + m * j]));
-                result = csv_strsubst(buffer, getCsvDefaultDecimal(), decimal);
+                    if (hasReal && (imagValue > 0))
+                    {
+                        strcat(StringValue, PlusStr);
+                    }
+                    else if (imagValue < 0)
+                    {
+                        if (hasReal)
+                        {
+                            strcat(StringValue, LessStr);
+                        }
+                        else
+                        {
+                            strcpy(StringValue, LessStr);
+                        }
+                    }
 
-                if (result)
-                {
-                    strcat(StringValue, result);
-                    FREE(result);
-                    result = NULL;
-                }
-                else
-                {
-                    sprintf(buffer, DEFAULT_CSV_WRITE_DOUBLE_FORMAT, fabs(pdValuesImag[i + m * j]));
-                    strcat(StringValue, buffer);
+                    sprintf(buffer, precisionFormat, fabs(imagValue));
+                    result = csv_strsubst(buffer, getCsvDefaultDecimal(), decimal);
+                    if (result)
+                    {
+                        if ((hasReal) || (imagValue < 0))
+                        {
+                            strcat(StringValue, result);
+                        }
+                        else
+                        {
+                            strcpy(StringValue, result);
+                        }
+                        FREE(result);
+                    }
+                    else
+                    {
+                        sprintf(StringValue, DEFAULT_CSV_WRITE_DOUBLE_FORMAT, imagValue);
+                    }
+                    strcat(StringValue, ComplexStr);
                 }
-                strcat(StringValue, ComplexStr);
             }
             else
             {
-                if ( signbit(pdValuesImag[i + m * j]) )
+                if (hasReal && (signbit(imagValue) == 0))
                 {
-                    // NegInfString
-                    strcat(StringValue, LessStr);
+                    strcat(StringValue, PlusStr);
                 }
-                else
+                else if (signbit(realValue) > 0)
                 {
-                    // InfString
-                    strcat(StringValue, PlusStr);
+                    if (hasReal)
+                    {
+                        strcat(StringValue, LessStr);
+                    }
+                    else
+                    {
+                        strcpy(StringValue, LessStr);
+                    }
                 }
                 strcat(StringValue, InfString);
                 strcat(StringValue, ComplexStr);
diff --git a/scilab/modules/spreadsheet/tests/nonreg_tests/bug_12360.dia.ref b/scilab/modules/spreadsheet/tests/nonreg_tests/bug_12360.dia.ref
new file mode 100644 (file)
index 0000000..1f94af7
--- /dev/null
@@ -0,0 +1,20 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Simon MARCHETTO
+//
+// This file is distributed under the same license as the Scilab package.
+// =============================================================================
+//
+// <-- Non-regression test for bug 12360 -->
+//
+// <-- Bugzilla URL -->
+// http://bugzilla.scilab.org/show_bug.cgi?id=12360
+//
+// <-- Short Description -->
+// Imaginary part of purely real complex is printed in CSV
+realMatrix = [0.211, 3, -4.0, 0.0];
+purelyRealComplexMatrix = complex(realMatrix);
+filename = fullfile(TMPDIR, 'bug_12360.csv');
+write_csv(purelyRealComplexMatrix, filename, ascii(9), ".", "%5.3f");
+path = SCI + "/modules/spreadsheet/tests/nonreg_tests";
+assert_checkfilesequal(filename, fullfile(path, "bug_12360.ref.csv"));
diff --git a/scilab/modules/spreadsheet/tests/nonreg_tests/bug_12360.ref.csv b/scilab/modules/spreadsheet/tests/nonreg_tests/bug_12360.ref.csv
new file mode 100644 (file)
index 0000000..8761243
--- /dev/null
@@ -0,0 +1 @@
+0.211  3.000   -4.000  0.000
diff --git a/scilab/modules/spreadsheet/tests/nonreg_tests/bug_12360.tst b/scilab/modules/spreadsheet/tests/nonreg_tests/bug_12360.tst
new file mode 100644 (file)
index 0000000..3601261
--- /dev/null
@@ -0,0 +1,23 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Simon MARCHETTO
+//
+// This file is distributed under the same license as the Scilab package.
+// =============================================================================
+//
+// <-- Non-regression test for bug 12360 -->
+//
+// <-- Bugzilla URL -->
+// http://bugzilla.scilab.org/show_bug.cgi?id=12360
+//
+// <-- Short Description -->
+// Imaginary part of purely real complex is printed in CSV
+
+realMatrix = [0.211, 3, -4.0, 0.0];
+purelyRealComplexMatrix = complex(realMatrix);
+
+filename = fullfile(TMPDIR, 'bug_12360.csv');
+write_csv(purelyRealComplexMatrix, filename, ascii(9), ".", "%5.3f");
+
+path = SCI + "/modules/spreadsheet/tests/nonreg_tests";
+assert_checkfilesequal(filename, fullfile(path, "bug_12360.ref.csv"));
\ No newline at end of file
index 59a75db..a682ac6 100644 (file)
@@ -19,31 +19,31 @@ m = m*(1+%i);
 filename=TMPDIR+"/test.txt";
 write_csv(m,filename);
 b=mgetl(filename);
-assert_checkequal(b, ["7+7i,-2-2i,9+9i";"1+1i,-8-8i,2+2i";"1+1i,-6-6i,0+0i"]);
+assert_checkequal(b, ["7+7i,-2-2i,9+9i";"1+1i,-8-8i,2+2i";"1+1i,-6-6i,0"]);
 b=csvRead(filename);
 assert_checkequal(b,m);
 write_csv([ 1  %nan*(1+%i)],filename);
 b=mgetl(filename);
-assert_checkequal(b,"1+0i,Nan+Nani");
+assert_checkequal(b,"1,Nan+Nani");
 b=csvRead(filename);
 assert_checkequal(b, [1  %nan*(1+%i)]);
 write_csv([ 1.1  %nan*(1+%i)],filename);
 b=mgetl(filename);
-assert_checkequal(b,"1.1000000000000001+0i,Nan+Nani");
+assert_checkequal(b,"1.1000000000000001,Nan+Nani");
 b=csvRead(filename);
 assert_checkequal(b, [1.1  %nan*(1+%i)]);
 write_csv([ 1  %inf*(1+%i)],filename)
 b=mgetl(filename);
-assert_checkequal(b,"1+0i,Inf+Infi");
+assert_checkequal(b,"1,Inf+Infi");
 b=csvRead(filename);
 assert_checkequal(b, [1  %inf*(1+%i)]);
 write_csv([ 1.1  %inf*(1+%i)],filename)
 b=mgetl(filename);
-assert_checkequal(b,"1.1000000000000001+0i,Inf+Infi");
+assert_checkequal(b,"1.1000000000000001,Inf+Infi");
 b=csvRead(filename);
 assert_checkequal(b, [1.1  %inf*(1+%i)]);
-write_csv([ 1.1  1+%i*%inf],filename) 
+write_csv([ 1.1  1+%i*%inf],filename)
 b=mgetl(filename);
-assert_checkequal(b,"1.1000000000000001+0i,Nan+Infi");
+assert_checkequal(b,"1.1000000000000001,Nan+Infi");
 b=csvRead(filename);
 assert_checkequal(b, [ 1.1  1+%i*%inf]);
index a89d8ba..a9aab6a 100644 (file)
@@ -20,37 +20,37 @@ m = m*(1+%i);
 filename=TMPDIR+"/test.txt";
 write_csv(m,filename);
 b=mgetl(filename);
-assert_checkequal(b, ["7+7i,-2-2i,9+9i";"1+1i,-8-8i,2+2i";"1+1i,-6-6i,0+0i"]);
+assert_checkequal(b, ["7+7i,-2-2i,9+9i";"1+1i,-8-8i,2+2i";"1+1i,-6-6i,0"]);
 b=csvRead(filename);
 assert_checkequal(b,m);
 
 write_csv([ 1  %nan*(1+%i)],filename);
 b=mgetl(filename);
-assert_checkequal(b,"1+0i,Nan+Nani");
+assert_checkequal(b,"1,Nan+Nani");
 b=csvRead(filename);
 assert_checkequal(b, [1  %nan*(1+%i)]);
 
 write_csv([ 1.1  %nan*(1+%i)],filename);
 b=mgetl(filename);
-assert_checkequal(b,"1.1000000000000001+0i,Nan+Nani");
+assert_checkequal(b,"1.1000000000000001,Nan+Nani");
 b=csvRead(filename);
 assert_checkequal(b, [1.1  %nan*(1+%i)]);
 
 
 write_csv([ 1  %inf*(1+%i)],filename)
 b=mgetl(filename);
-assert_checkequal(b,"1+0i,Inf+Infi");
+assert_checkequal(b,"1,Inf+Infi");
 b=csvRead(filename);
 assert_checkequal(b, [1  %inf*(1+%i)]);
 
 write_csv([ 1.1  %inf*(1+%i)],filename)
 b=mgetl(filename);
-assert_checkequal(b,"1.1000000000000001+0i,Inf+Infi");
+assert_checkequal(b,"1.1000000000000001,Inf+Infi");
 b=csvRead(filename);
 assert_checkequal(b, [1.1  %inf*(1+%i)]);
 
-write_csv([ 1.1  1+%i*%inf],filename) 
+write_csv([ 1.1  1+%i*%inf],filename)
 b=mgetl(filename);
-assert_checkequal(b,"1.1000000000000001+0i,Nan+Infi");
+assert_checkequal(b,"1.1000000000000001,Nan+Infi");
 b=csvRead(filename);
 assert_checkequal(b, [ 1.1  1+%i*%inf]);
diff --git a/scilab/modules/spreadsheet/tests/unit_tests/complex.csv b/scilab/modules/spreadsheet/tests/unit_tests/complex.csv
new file mode 100644 (file)
index 0000000..3471b4b
--- /dev/null
@@ -0,0 +1,5 @@
+0.211  -3.140  0.000
+1.000  40.780i -4.530i
+5.000+1.000i   5.000+3.500i    67.000-15.000i
+-Inf   Inf+2.000i      Inf-5.000i
+Nan    Nan+Nani        Nan+Nani
index 93eba4c..1f346a4 100644 (file)
@@ -1,6 +1,7 @@
 // =============================================================================
 // Copyright (C) 2011 - INRIA - Michael Baudin
 // Copyright (C) 2010 - 2011 - INRIA - Allan CORNET
+// Copyright (C) 2013 - Scilab Enterprises - Simon MARCHETTO
 // =============================================================================
 // <-- JVM NOT MANDATORY -->
 path = SCI+"/modules/spreadsheet/tests/unit_tests/";
@@ -103,4 +104,13 @@ expected = [
 ];
 assert_checkequal ( Mstr , expected );
 // =============================================================================
-
+// complex matrix
+complexMatrix = [0.211, -3.14, 0; ..
+1+0*%i, 40.78*%i, -4.53*%i; ..
+5+%i, 5+3.5*%i, 67.0-15*%i; ..
+-%inf, %inf+2*%i, %inf-5.0*%i; ..
+%nan, %nan*%i, %nan+%nan*%i];
+//TODO check values with %inf*%i (currently bug on interpreting %inf*%i)
+filename = fullfile(TMPDIR, "complex.csv");
+write_csv(complexMatrix, filename, ascii(9), ".", "%5.3f");
+assert_checkfilesequal(filename , fullfile(path, "complex.csv"));
index 59c058b..1bd5ed9 100644 (file)
@@ -1,6 +1,7 @@
 // =============================================================================
 // Copyright (C) 2011 - INRIA - Michael Baudin
 // Copyright (C) 2010 - 2011 - INRIA - Allan CORNET
+// Copyright (C) 2013 - Scilab Enterprises - Simon MARCHETTO
 // =============================================================================
 // <-- JVM NOT MANDATORY -->
 path = SCI+"/modules/spreadsheet/tests/unit_tests/";
@@ -104,5 +105,15 @@ expected = [
 assert_checkequal ( Mstr , expected );
 // =============================================================================
 
+// complex matrix
+complexMatrix = [0.211, -3.14, 0; ..
+1+0*%i, 40.78*%i, -4.53*%i; ..
+5+%i, 5+3.5*%i, 67.0-15*%i; ..
+-%inf, %inf+2*%i, %inf-5.0*%i; ..
+%nan, %nan*%i, %nan+%nan*%i];
 
+//TODO check values with %inf*%i (currently bug on interpreting %inf*%i)
 
+filename = fullfile(TMPDIR, "complex.csv");
+write_csv(complexMatrix, filename, ascii(9), ".", "%5.3f");
+assert_checkfilesequal(filename , fullfile(path, "complex.csv"));