fix sum on (u)int64 with big values ( > 53 bits ) 35/18935/2
Antoine ELIAS [Tue, 24 Jan 2017 11:43:15 +0000 (12:43 +0100)]
Change-Id: I760cf37d0cc70812c3a8cc06208110142f82a1bc

scilab/modules/elementary_functions/sci_gateway/cpp/sci_sum.cpp
scilab/modules/elementary_functions/src/cpp/sum.hxx
scilab/modules/elementary_functions/tests/unit_tests/sum.dia.ref [deleted file]
scilab/modules/elementary_functions/tests/unit_tests/sum.tst

index 0d87ffc..945bf3f 100644 (file)
@@ -14,6 +14,7 @@
  * along with this program.
  *
  */
+
 /*--------------------------------------------------------------------------*/
 #include "elem_func_gw.hxx"
 #include "function.hxx"
@@ -35,13 +36,15 @@ extern "C"
 /*--------------------------------------------------------------------------*/
 types::Function::ReturnValue sci_sum(types::typed_list &in, int _iRetCount, types::typed_list &out)
 {
-    types::Double* pDblIn       = NULL;
-    types::Double* pDblOut      = NULL;
-    types::Polynom* pPolyIn     = NULL;
-    types::Polynom* pPolyOut    = NULL;
+    types::Double* pDblIn = NULL;
+    types::Double* pDblOut = NULL;
+    types::GenericType* pIntIn = NULL;
+    types::GenericType* pIntOut = NULL;
+    types::Polynom* pPolyIn = NULL;
+    types::Polynom* pPolyOut = NULL;
 
-    int iOrientation    = 0;
-    int iOuttype        = 1; // 1 = native | 2 = double (type of output value)
+    int iOrientation = 0;
+    int iOuttype = 1; // 1 = native | 2 = double (type of output value)
 
     if (in.size() < 1 || in.size() > 3)
     {
@@ -55,73 +58,18 @@ types::Function::ReturnValue sci_sum(types::typed_list &in, int _iRetCount, type
         return types::Function::Error;
     }
 
-    bool isCopy = true;
-    /***** get data *****/
-    switch (in[0]->getType())
+
+    //call overload for non manage types
+    if (in[0]->isDouble() == false && in[0]->isInt() == false && in[0]->isPoly() == false && in[0]->isBool() == false)
     {
-        case types::InternalType::ScilabDouble:
-        {
-            pDblIn = in[0]->getAs<types::Double>();
-            isCopy = false;
-            break;
-        }
-        case types::InternalType::ScilabBool:
-        {
-            pDblIn = getAsDouble(in[0]->getAs<types::Bool>());
-            iOuttype = 2;
-            break;
-        }
-        case types::InternalType::ScilabPolynom:
-        {
-            pPolyIn = in[0]->getAs<types::Polynom>();
-            break;
-        }
-        case types::InternalType::ScilabInt8:
-        {
-            pDblIn = getAsDouble(in[0]->getAs<types::Int8>());
-            break;
-        }
-        case types::InternalType::ScilabInt16:
-        {
-            pDblIn = getAsDouble(in[0]->getAs<types::Int16>());
-            break;
-        }
-        case types::InternalType::ScilabInt32:
-        {
-            pDblIn = getAsDouble(in[0]->getAs<types::Int32>());
-            break;
-        }
-        case types::InternalType::ScilabInt64:
-        {
-            pDblIn = getAsDouble(in[0]->getAs<types::Int64>());
-            break;
-        }
-        case types::InternalType::ScilabUInt8:
-        {
-            pDblIn = getAsDouble(in[0]->getAs<types::UInt8>());
-            break;
-        }
-        case types::InternalType::ScilabUInt16:
-        {
-            pDblIn = getAsDouble(in[0]->getAs<types::UInt16>());
-            break;
-        }
-        case types::InternalType::ScilabUInt32:
-        {
-            pDblIn = getAsDouble(in[0]->getAs<types::UInt32>());
-            break;
-        }
-        case types::InternalType::ScilabUInt64:
-        {
-            pDblIn = getAsDouble(in[0]->getAs<types::UInt64>());
-            break;
-        }
-        default:
-        {
-            std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_sum";
-            types::Function::ReturnValue ret = Overload::call(wstFuncName, in, _iRetCount, out);
-            return ret;
-        }
+        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_sum";
+        types::Function::ReturnValue ret = Overload::call(wstFuncName, in, _iRetCount, out);
+        return ret;
+    }
+
+    if (in[0]->isBool())
+    {
+        iOuttype = 2;
     }
 
     if (in.size() >= 2)
@@ -132,11 +80,6 @@ types::Function::ReturnValue sci_sum(types::typed_list &in, int _iRetCount, type
 
             if (pDbl->isScalar() == false)
             {
-                if (isCopy && pDblIn)
-                {
-                    pDblIn->killMe();
-                }
-
                 Scierror(999, _("%s: Wrong value for input argument #%d: A positive scalar expected.\n"), "sum", 2);
                 return types::Function::Error;
             }
@@ -145,11 +88,6 @@ types::Function::ReturnValue sci_sum(types::typed_list &in, int _iRetCount, type
 
             if (iOrientation <= 0)
             {
-                if (isCopy && pDblIn)
-                {
-                    pDblIn->killMe();
-                }
-
                 Scierror(999, _("%s: Wrong value for input argument #%d: A positive scalar expected.\n"), "sum", 2);
                 return types::Function::Error;
             }
@@ -160,11 +98,6 @@ types::Function::ReturnValue sci_sum(types::typed_list &in, int _iRetCount, type
 
             if (pStr->isScalar() == false)
             {
-                if (isCopy && pDblIn)
-                {
-                    pDblIn->killMe();
-                }
-
                 Scierror(999, _("%s: Wrong size for input argument #%d: A scalar string expected.\n"), "sum", 2);
                 return types::Function::Error;
             }
@@ -193,6 +126,11 @@ types::Function::ReturnValue sci_sum(types::typed_list &in, int _iRetCount, type
                     iDims = pDblIn->getDims();
                     piDimsArray = pDblIn->getDimsArray();
                 }
+                else if (pIntIn)
+                {
+                    iDims = pIntIn->getDims();
+                    piDimsArray = pIntIn->getDimsArray();
+                }
                 else
                 {
                     iDims = pPolyIn->getDims();
@@ -229,22 +167,12 @@ types::Function::ReturnValue sci_sum(types::typed_list &in, int _iRetCount, type
                     pstrExpected = "\"*\",\"r\",\"c\",\"m\"";
                 }
 
-                if (isCopy && pDblIn)
-                {
-                    pDblIn->killMe();
-                }
-
                 Scierror(999, _("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"), "sum", 2, pstrExpected);
                 return types::Function::Error;
             }
         }
         else
         {
-            if (isCopy && pDblIn)
-            {
-                pDblIn->killMe();
-            }
-
             Scierror(999, _("%s: Wrong type for input argument #%d: A real matrix or a string expected.\n"), "sum", 2);
             return types::Function::Error;
         }
@@ -254,11 +182,6 @@ types::Function::ReturnValue sci_sum(types::typed_list &in, int _iRetCount, type
     {
         if (in[2]->isString() == false)
         {
-            if (isCopy && pDblIn)
-            {
-                pDblIn->killMe();
-            }
-
             Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), "sum", 3);
             return types::Function::Error;
         }
@@ -267,11 +190,6 @@ types::Function::ReturnValue sci_sum(types::typed_list &in, int _iRetCount, type
 
         if (pStr->isScalar() == false)
         {
-            if (isCopy && pDblIn)
-            {
-                pDblIn->killMe();
-            }
-
             Scierror(999, _("%s: Wrong size for input argument #%d: A scalar string expected.\n"), "sum", 3);
             return types::Function::Error;
         }
@@ -288,16 +206,96 @@ types::Function::ReturnValue sci_sum(types::typed_list &in, int _iRetCount, type
         }
         else
         {
-            if (isCopy && pDblIn)
-            {
-                pDblIn->killMe();
-            }
-
             Scierror(999, _("%s: Wrong value for input argument #%d: %s or %s expected.\n"), "sum", 3, "\"native\"", "\"double\"");
             return types::Function::Error;
         }
     }
 
+    bool isCopy = true;
+    /***** get data *****/
+    switch (in[0]->getType())
+    {
+        case types::InternalType::ScilabDouble:
+        {
+            pDblIn = in[0]->getAs<types::Double>();
+            isCopy = false;
+            break;
+        }
+        case types::InternalType::ScilabBool:
+        {
+            pDblIn = getAsDouble(in[0]->getAs<types::Bool>());
+            break;
+        }
+        case types::InternalType::ScilabPolynom:
+        {
+            pPolyIn = in[0]->getAs<types::Polynom>();
+            isCopy = false;
+            break;
+        }
+        case types::InternalType::ScilabInt8:
+        case types::InternalType::ScilabInt16:
+        case types::InternalType::ScilabInt32:
+        case types::InternalType::ScilabInt64:
+        case types::InternalType::ScilabUInt8:
+        case types::InternalType::ScilabUInt16:
+        case types::InternalType::ScilabUInt32:
+        case types::InternalType::ScilabUInt64:
+        {
+            if (iOuttype == 1)
+            {
+                pIntIn = in[0]->getAs<types::GenericType>();
+                isCopy = false;
+            }
+            else
+            {
+                switch (in[0]->getType())
+                {
+                    case types::InternalType::ScilabInt8:
+                    {
+                        pDblIn = getAsDouble(in[0]->getAs<types::Int8>());
+                        break;
+                    }
+                    case types::InternalType::ScilabInt16:
+                    {
+                        pDblIn = getAsDouble(in[0]->getAs<types::Int16>());
+                        break;
+                    }
+                    case types::InternalType::ScilabInt32:
+                    {
+                        pDblIn = getAsDouble(in[0]->getAs<types::Int32>());
+                        break;
+                    }
+                    case types::InternalType::ScilabInt64:
+                    {
+                        pDblIn = getAsDouble(in[0]->getAs<types::Int64>());
+                        break;
+                    }
+                    case types::InternalType::ScilabUInt8:
+                    {
+                        pDblIn = getAsDouble(in[0]->getAs<types::UInt8>());
+                        break;
+                    }
+                    case types::InternalType::ScilabUInt16:
+                    {
+                        pDblIn = getAsDouble(in[0]->getAs<types::UInt16>());
+                        break;
+                    }
+                    case types::InternalType::ScilabUInt32:
+                    {
+                        pDblIn = getAsDouble(in[0]->getAs<types::UInt32>());
+                        break;
+                    }
+                    case types::InternalType::ScilabUInt64:
+                    {
+                        pDblIn = getAsDouble(in[0]->getAs<types::UInt64>());
+                        break;
+                    }
+                }
+            }
+            break;
+        }
+    }
+
     /***** perform operation *****/
     if (pDblIn)
     {
@@ -337,12 +335,66 @@ types::Function::ReturnValue sci_sum(types::typed_list &in, int _iRetCount, type
             }
         }
     }
+    else if (pIntIn)
+    {
+        iOuttype = 1;
+        if (iOrientation > pIntIn->getDims())
+        {
+            pIntOut = pIntIn;
+        }
+        else
+        {
+            switch (pIntIn->getType())
+            {
+                case types::InternalType::ScilabInt8:
+                {
+                    pIntOut = sum(pIntIn->getAs<types::Int8>(), iOrientation);
+                    break;
+                }
+                case types::InternalType::ScilabInt16:
+                {
+                    pIntOut = sum(pIntIn->getAs<types::Int16>(), iOrientation);
+                    break;
+                }
+                case types::InternalType::ScilabInt32:
+                {
+                    pIntOut = sum(pIntIn->getAs<types::Int32>(), iOrientation);
+                    break;
+                }
+                case types::InternalType::ScilabInt64:
+                {
+                    pIntOut = sum(pIntIn->getAs<types::Int64>(), iOrientation);
+                    break;
+                }
+                case types::InternalType::ScilabUInt8:
+                {
+                    pIntOut = sum(pIntIn->getAs<types::UInt8>(), iOrientation);
+                    break;
+                }
+                case types::InternalType::ScilabUInt16:
+                {
+                    pIntOut = sum(pIntIn->getAs<types::UInt16>(), iOrientation);
+                    break;
+                }
+                case types::InternalType::ScilabUInt32:
+                {
+                    pIntOut = sum(pIntIn->getAs<types::UInt32>(), iOrientation);
+                    break;
+                }
+                case types::InternalType::ScilabUInt64:
+                {
+                    pIntOut = sum(pIntIn->getAs<types::UInt64>(), iOrientation);
+                    break;
+                }
+            }
+        }
+    }
     else if (pPolyIn)
     {
         iOuttype = 1;
         if (iOrientation > pPolyIn->getDims())
         {
-            pPolyOut = pPolyIn->getAs<types::Polynom>();
+            pPolyOut = pPolyIn;
         }
         else
         {
@@ -373,46 +425,6 @@ types::Function::ReturnValue sci_sum(types::typed_list &in, int _iRetCount, type
                 out.push_back(pPolyOut);
                 break;
             }
-            case types::InternalType::ScilabInt8:
-            {
-                out.push_back(toInt<types::Int8>(pDblOut));
-                break;
-            }
-            case types::InternalType::ScilabInt16:
-            {
-                out.push_back(toInt<types::Int16>(pDblOut));
-                break;
-            }
-            case types::InternalType::ScilabInt32:
-            {
-                out.push_back(toInt<types::Int32>(pDblOut));
-                break;
-            }
-            case types::InternalType::ScilabInt64:
-            {
-                out.push_back(toInt<types::Int64>(pDblOut));
-                break;
-            }
-            case types::InternalType::ScilabUInt8:
-            {
-                out.push_back(toInt<types::UInt8>(pDblOut));
-                break;
-            }
-            case types::InternalType::ScilabUInt16:
-            {
-                out.push_back(toInt<types::UInt16>(pDblOut));
-                break;
-            }
-            case types::InternalType::ScilabUInt32:
-            {
-                out.push_back(toInt<types::UInt32>(pDblOut));
-                break;
-            }
-            case types::InternalType::ScilabUInt64:
-            {
-                out.push_back(toInt<types::UInt64>(pDblOut));
-                break;
-            }
             default:
                 return types::Function::Error;
         }
@@ -424,7 +436,19 @@ types::Function::ReturnValue sci_sum(types::typed_list &in, int _iRetCount, type
     }
     else
     {
-        out.push_back(pDblOut);
+        if (pPolyOut)
+        {
+            out.push_back(pPolyOut);
+        }
+        else if (pIntOut)
+        {
+            out.push_back(pIntOut);
+        }
+        else
+        {
+            out.push_back(pDblOut);
+        }
+
     }
 
     return types::Function::OK;
index 72c9d3b..7bbd371 100644 (file)
@@ -1,7 +1,7 @@
 /*
-*  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-*  Copyright (C) 2012 - Scilab Enterprises - Cedric Delamarre
-*
+ *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ *  Copyright (C) 2012 - Scilab Enterprises - Cedric Delamarre
+ *
  * Copyright (C) 2012 - 2016 - Scilab Enterprises
  *
  * This file is hereby licensed under the terms of the GNU GPL v2.0,
@@ -10,8 +10,8 @@
  * and continues to be available under such terms.
  * For more information, see the COPYING file which you should have received
  * along with this program.
-*
-*/
+ *
+ */
 
 #ifndef __SUM_H__
 #define __SUM_H__
@@ -27,4 +27,67 @@ extern "C"
 ELEMENTARY_FUNCTIONS_IMPEXP types::Double*  sum(types::Double*  pIn, int iOrientation);
 ELEMENTARY_FUNCTIONS_IMPEXP types::Polynom* sum(types::Polynom* pIn, int iOrientation);
 
+template <class T>
+T* sum(T* pIn, int iOrientation)
+{
+    T* pOut = NULL;
+    typename T::type* pR = pIn->get();
+
+    if (iOrientation == 0) // all
+    {
+        typename T::type r = 0;
+
+        for (int i = 0; i < pIn->getSize(); i++)
+        {
+            r += pR[i];
+        }
+
+        pOut = new T(r);
+    }
+    else // sum on one dimension
+    {
+        // create output dimensions
+        int iDims = pIn->getDims();
+        int* piDims = new int[iDims];
+
+        for (int i = 0; i < iDims; i++)
+        {
+            piDims[i] = pIn->getDimsArray()[i];
+        }
+
+        piDims[iOrientation - 1] = 1;
+
+        // create output variable
+        pOut = new T(iDims, piDims);
+        typename T::type* pROut = pOut->get();
+
+        // init output
+        for (int i = 0; i < pOut->getSize(); ++i)
+        {
+            pROut[i] = 0;
+        }
+
+        delete[] piDims;
+        piDims = NULL;
+
+        // perform operations
+        int* piIndex = new int[iDims];
+        for (int i = 0; i < pIn->getSize(); i++)
+        {
+            //get array of dim
+            pIn->getIndexes(i, piIndex);
+
+            //convert indexes for result
+            piIndex[iOrientation - 1] = 0;
+            int iIndex = pOut->getIndex(piIndex);
+            pROut[iIndex] += pR[i];
+        }
+
+        delete[] piIndex;
+        piIndex = NULL;
+    }
+
+    return pOut;
+}
+
 #endif /* __SUM_H__ */
diff --git a/scilab/modules/elementary_functions/tests/unit_tests/sum.dia.ref b/scilab/modules/elementary_functions/tests/unit_tests/sum.dia.ref
deleted file mode 100644 (file)
index 3d656c0..0000000
+++ /dev/null
@@ -1,285 +0,0 @@
-// =============================================================================
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) 2010 - INRIA - Serge Steer
-// Copyright (C) 2013 - Scilab Enterprises - Adeline CARNIS
-//
-//  This file is distributed under the same license as the Scilab package.
-// =============================================================================
-// <-- CLI SHELL MODE -->
-// Check error
-d=[1 10;254 9];
-assert_checkfalse(execstr("sum(d, ""orient"")"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"), "sum",2,"""*"",""r"",""c"",""m"",""native"",""double""");
-assert_checkerror("sum(d, ""orient"")", refMsg);
-assert_checkfalse(execstr("sum(d, [""r"", ""c""])"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong size for input argument #%d: A scalar string expected.\n"), "sum", 2);
-assert_checkerror("sum(d, [""r"", ""c""])", refMsg);
-assert_checkfalse(execstr("sum(d, ""e"", ""double"")"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"), "sum",2,"""*"",""r"",""c"",""m""");
-assert_checkerror("sum(d, ""e"", ""double"")", refMsg);
-assert_checkfalse(execstr("sum(d, -1.5, ""double"")"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong value for input argument #%d: A positive scalar expected.\n"), "sum", 2);
-assert_checkerror("sum(d, -1.5, ""double"")", refMsg);
-//==============================================================================
-// matrices of integer
-i = uint8(d);
-assert_checkfalse(execstr("sum(i, ""orient"")"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"), "sum",2,"""*"",""r"",""c"",""m"",""native"",""double""");
-assert_checkerror("sum(i, ""orient"")", refMsg);
-assert_checkfalse(execstr("sum(i, [""r"", ""c""])"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong size for input argument #%d: A scalar string expected.\n"), "sum", 2);
-assert_checkerror("sum(i, [""r"", ""c""])", refMsg);
-assert_checkfalse(execstr("sum(i,""r"", ""nat"")"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong value for input argument #%d: %s or %s expected.\n"),"sum", 3, """native""", """double""");
-assert_checkerror("sum(i,""r"", ""nat"")", refMsg);
-assert_checkfalse(execstr("sum(i,""r"", [""nat"" ""dble""])"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong size for input argument #%d: A scalar string expected.\n"), "sum", 3);
-assert_checkerror("sum(i,""r"", [""nat"" ""dble""])", refMsg);
-assert_checkfalse(execstr("sum(i,""orient"", ""t"")"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"), "sum",2,"""*"",""r"",""c"",""m""");
-assert_checkerror("sum(i,""orient"", ""t"")", refMsg);
-assert_checkfalse(execstr("sum(i,1,1)"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong type for input argument #%d: string expected.\n"), "sum", 3);
-assert_checkerror("sum(i,1,1)", refMsg);
-//==============================================================================
-// sparse matrices
-d = sparse(d);
-assert_checkfalse(execstr("sum(d, ""orient"")","errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"), "sum",2,"""*"",""r"",""c"",""m""");
-assert_checkerror("sum(d, ""orient"")", refMsg);
-assert_checkfalse(execstr("sum(d, [""r"", ""c""])"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong size for input argument #%d: string expected.\n"),"sum",2);
-assert_checkerror("sum(d, [""r"", ""c""])", refMsg);
-//==============================================================================
-// boolean matrices
-d = [%f %t;%t %f];
-assert_checkfalse(execstr("sum(d, ""orient"")"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),"sum",2,"""*"",""r"",""c"",""m"",""native"",""double""");
-assert_checkerror("sum(d, ""orient"")", refMsg);
-assert_checkfalse(execstr("sum(d, [""r"", ""c""])"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong size for input argument #%d: A scalar string expected.\n"),"sum",2);
-assert_checkerror("sum(d, [""r"", ""c""])", refMsg);
-assert_checkfalse(execstr("sum(d,""r"", ""nat"")"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong value for input argument #%d: %s or %s expected.\n"),"sum", 3, """native""", """double""");
-assert_checkerror("sum(d,""r"", ""nat"")", refMsg);
-assert_checkfalse(execstr("sum(d,""r"", [""nat"" ""dble""])"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong size for input argument #%d: A scalar string expected.\n"),"sum",3);
-assert_checkerror("sum(d,""r"", [""nat"" ""dble""])", refMsg);
-assert_checkfalse(execstr("sum(d,""orient"", ""t"")"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),..
-"sum",2,"""*"",""r"",""c"",""m""");
-assert_checkerror("sum(d,""orient"", ""t"")", refMsg);
-assert_checkfalse(execstr("sum(d,1,1)"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong type for input argument #%d: string expected.\n"),"sum",3);
-assert_checkerror("sum(d,1,1)", refMsg);
-//==============================================================================
-// sparse boolean matrices
-d = sparse(d);
-assert_checkfalse(execstr("sum(d, ""orient"")"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),..
-"sum",2,"""*"",""r"",""c"",""m""");
-assert_checkerror("sum(d, ""orient"")", refMsg);
-assert_checkfalse(execstr("sum(d, [""r"", ""c""])"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong size for input argument #%d: string expected.\n"),"sum",2);
-assert_checkerror("sum(d, [""r"", ""c""])", refMsg);
-assert_checkfalse(execstr("sum(d,""r"", ""nat"")"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong value for input argument #%d: ""%s"" or ""%s"" expected.\n"),"sum", 3, "native", "double");
-assert_checkerror("sum(d,""r"", ""nat"")", refMsg);
-assert_checkfalse(execstr("sum(d,""r"", [""nat"" ""dble""])"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong size for input argument #%d: string expected.\n"),"sum",3);
-assert_checkerror("sum(d,""r"", [""nat"" ""dble""])", refMsg);
-assert_checkfalse(execstr("sum(d,""orient"", ""t"")"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),..
-"sum",2,"""*"",""r"",""c"",""m""");
-assert_checkerror("sum(d,""orient"", ""t"")", refMsg);
-assert_checkfalse(execstr("sum(d,1,1)"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong type for input argument #%d: string expected.\n"),"sum",3);
-assert_checkerror("sum(d,1,1)", refMsg);
-//==============================================================================
-// hypermatrices
-d = rand(2,2,2);
-assert_checkfalse(execstr("sum(d, ""orient"")"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"), "sum",2,"""*"",""r"",""c"",""m"",""native"",""double""");
-assert_checkerror("sum(d, ""orient"")", refMsg);
-assert_checkfalse(execstr("sum(d, [""r"", ""c""])"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong size for input argument #%d: A scalar string expected.\n"),"sum",2);
-assert_checkerror("sum(d, [""r"", ""c""])", refMsg);
-//==============================================================================
-// polynome
-s = poly(0, "s");
-d = [s s^2;s*%i 1];
-assert_checkfalse(execstr("sum(d, ""orient"")"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),"sum",2,"""*"",""r"",""c"",""m"",""native"",""double""");
-assert_checkerror("sum(d, ""orient"")", refMsg);
-assert_checkfalse(execstr("sum(d, [""r"", ""c""])"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong size for input argument #%d: A scalar string expected.\n"),"sum",2);
-assert_checkerror("sum(d, [""r"", ""c""])", refMsg);
-//==============================================================================
-// rational matrices
-s = poly(0, "s");
-d = [1/s 1/s^2; 1/s 1];
-assert_checkfalse(execstr("sum(d, ""orient"")"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),..
-"sum",2,"""*"",""r"",""c"",""m""");
-assert_checkerror("sum(d, ""orient"")", refMsg);
-assert_checkfalse(execstr("sum(d, [""r"", ""c""])"   ,"errcatch") == 0);
-refMsg = msprintf(_("%s: Wrong size for input argument #%d: string expected.\n"),"sum",2);
-assert_checkerror("sum(d, [""r"", ""c""])", refMsg);
-//empty matrices
-T=list(list(),list("native"),list("double"));
-for typ=T
-    assert_checkequal(sum([], typ(:)), 0);
-    assert_checkequal(sum([], "*", typ(:)), 0);
-    assert_checkequal(sum([], 1, typ(:)), []);
-    assert_checkequal(sum([], 2, typ(:)), []);
-    assert_checkequal(sum([], 3, typ(:)), []);
-end
-//=======================================================================
-//float matrices
-d=[1 10;254 9];
-T=list(list(),list("native"),list("double"));
-for typ=T
-    assert_checkequal(sum(d, typ(:)), 274);
-    assert_checkequal(sum(d, "*", typ(:)), 274);
-    assert_checkequal(sum(d, 1, typ(:)), [255 19]);
-    assert_checkequal(sum(d, 2, typ(:)), [11; 263]);
-    assert_checkequal(sum(d, 3, typ(:)), d);
-end
-//hypermatrices of floats
-d(1,1,2)=1;
-for typ=T
-    assert_checkequal(sum(d, typ(:)), 275);
-    assert_checkequal(sum(d, "*", typ(:)), 275);
-    assert_checkequal(sum(d, 1, typ(:)), matrix([255;19;1;0], [1,2,2]));
-    assert_checkequal(sum(d, 2, typ(:)), matrix([11;263;1;0], [2,1,2]));
-    assert_checkequal(sum(d, 3, typ(:)), [2,10;254,9]);
-    assert_checkequal(sum(d, 5, typ(:)), d);
-end
-//=======================================================================
-//matrices of short integers
-// Caution : uint8([11;263]) is equal to uint8([11;7])
-i=uint8([1 10;254 9]);
-T = list(list(),list("native"));
-for typ=T
-    assert_checkequal(sum(i, typ(:)), uint8(274));
-    assert_checkequal(sum(i, "*", typ(:)), uint8(274));
-    assert_checkequal(sum(i, 1, typ(:)), uint8([255 19]));
-    assert_checkequal(sum(i, 2, typ(:)), uint8([11;263]));
-    assert_checkequal(sum(i, 3, typ(:)), double(i));
-end
-assert_checkequal(sum(i, "double"), 274);
-assert_checkequal(sum(i, "*", "double"), 274);
-assert_checkequal(sum(i, 1, "double"), [255 19]);
-assert_checkequal(sum(i, 2, "double"), [11; 263]);
-assert_checkequal(sum(i, 3, "double"), double(i));
-//with hypermatrices
-i(1,1,2)=uint8(1);
-T = list(list(),list("native"));
-for typ=T
-    assert_checkequal(sum(i, typ(:)), uint8(275));
-    assert_checkequal(sum(i, "*", typ(:)), uint8(275));
-    assert_checkequal(sum(i, 1, typ(:)), matrix(uint8([255;19;1;0]), [1,2,2]));
-    assert_checkequal(sum(i, 2, typ(:)), matrix(uint8([11;263;1;0]), [2,1,2]));
-    assert_checkequal(sum(i, 3, typ(:)), uint8([2,10;254,9]));
-    assert_checkequal(sum(i, 5, typ(:)), double(i));
-end
-assert_checkequal(sum(i, "double"), 275);
-assert_checkequal(sum(i, "*", "double"), 275);
-assert_checkequal(sum(i, 1, "double"), matrix([255;19;1;0], [1,2,2]));
-assert_checkequal(sum(i, 2, "double"), matrix([11;263;1;0], [2,1,2]));
-assert_checkequal(sum(i, 3, "double"), [2,10;254,9]);
-//=======================================================================
-//Matrices of polynomials
-s=%s;
-p=[s s+1;s^2 0];
-T=list(list(),list("native"),list("double"));
-for typ=T
-    assert_checkequal(sum(p, typ(:)), 1+2*s+s^2);
-    assert_checkequal(sum(p, "*", typ(:)), 1+2*s+s^2);
-    assert_checkequal(sum(p, 1, typ(:)), [s + s^2, 1 + s]);
-    assert_checkequal(sum(p, 2, typ(:)), [1+2*s;s^2]);
-    assert_checkequal(sum(p, 3, typ(:)), p);
-end
-//with hypermatrices
-p(1,1,2)=-1;
-for typ=T
-    assert_checkequal(sum(p, typ(:)), 2*s+s^2);
-    assert_checkequal(sum(p, "*", typ(:)), 2*s+s^2);
-    assert_checkequal(sum(p, 1, typ(:)), matrix([s+s^2;1+s;-1;0*s], [1,2,2]));
-    assert_checkequal(sum(p, 2, typ(:)), matrix([1+2*s;s^2;-1;0*s], [2,1,2]));
-    assert_checkequal(sum(p, 3, typ(:)), [-1+s,1+s;s^2,0]);
-    assert_checkequal(sum(p, 5, typ(:)), p);
-end
-//=======================================================================
-//Matrices of rationals
-s=%s;
-r=1.0 ./[s s+1;s^2 1];
-T=list(list(),list("native"),list("double"));
-for typ=T
-    assert_checkequal(sum(r, typ(:)), (1+2*s+3*s^2+s^3)./(s^2+s^3));
-    assert_checkequal(sum(r, "*", typ(:)), (1+2*s+3*s^2+s^3)./(s^2+s^3));
-    assert_checkequal(sum(r, 1, typ(:)), [1+s,2+s]./[s^2,1+s]);
-    assert_checkequal(sum(r, 2, typ(:)), [1+2*s;1+s^2]./[s+s^2;s^2]);
-    assert_checkequal(sum(r, 3, typ(:)), r);
-end
-//=======================================================================
-///Matrices of boolean
-b=[%t %t %f %t];
-T = list(list(),list("double"));
-for typ=T
-    assert_checkequal(sum(b, typ(:)), 3);
-    assert_checkequal(sum(b, "*", typ(:)), 3);
-    assert_checkequal(sum(b, 1, typ(:)), [1 1 0 1]);
-    assert_checkequal(sum(b, 2, typ(:)), 3);
-    assert_checkequal(sum(b, 3, typ(:)), double(b));
-end
-assert_checkequal(sum(b, "native"), %t);
-assert_checkequal(sum(b, "*", "native"), %t);
-assert_checkequal(sum(b, 1, "native"), b);
-assert_checkequal(sum(b, 2, "native"), %t);
-assert_checkequal(sum(b, 3, "native"), b);
-//with hypermatrices
-b=[%t %t %f %t];b(1,1,2)=%f;
-T = list(list(),list("double"));
-for typ=T
-    assert_checkequal(sum(b, typ(:)), 3);
-    assert_checkequal(sum(b, "*", typ(:)), 3);
-    assert_checkequal(sum(b, 1, typ(:)), matrix([1;1;0;1;0;0;0;0], [1,4,2]));
-    assert_checkequal(sum(b, 2, typ(:)), matrix([3;0], [1,1,2]));
-    assert_checkequal(sum(b, 3, typ(:)), [1 1 0 1]);
-    assert_checkequal(sum(b, 5, typ(:)), double(b));
-end
-assert_checkequal(sum(b, "native"), %t);
-assert_checkequal(sum(b, "*", "native"), %t);
-assert_checkequal(sum(b, 1, "native"), matrix([%t;%t;%f;%t;%f;%f;%f;%f], [1,4,2]));
-assert_checkequal(sum(b, 2, "native"), matrix([%t;%f], [1,1,2]));
-assert_checkequal(sum(b, 3, "native"), [%t %t %f %t]);
-assert_checkequal(sum(b, 5, "native"), b);
-//=======================================================================
-//sparse matrices of floats
-s=sparse([1 10 0;0 254 9]);
-T=list(list(),list("native"),list("double"));
-for typ=T
-    assert_checkequal(sum(s, typ(:)), 274);
-    assert_checkequal(sum(s, "*", typ(:)), 274);
-    assert_checkequal(sum(s, 1, typ(:)), sparse([1,264,9]));
-    assert_checkequal(sum(s, 2, typ(:)), sparse([11;263]));
-    assert_checkequal(sum(s, 3, typ(:)), s);
-end
-//=======================================================================
-//sparse  matrices of boolean
-bs=sparse([1 10 0;0 254 9])<>0;
-T = list(list(),list("double"));
-for typ=T
-    assert_checkequal(sum(bs, typ(:)), 4);
-    assert_checkequal(sum(bs, "*", typ(:)), 4);
-    assert_checkequal(sum(bs, 1, typ(:)), sparse([1,2,1]));
-    assert_checkequal(sum(bs, 2, typ(:)), sparse([2;2]));
-    assert_checkequal(sum(bs, 3, typ(:)), bool2s(bs));
-end
-assert_checkequal(sum(bs, "native"), %t);
-assert_checkequal(sum(bs, "*", "native"), %t);
-assert_checkequal(sum(bs, 1, "native"), sparse([%t,%t,%t]));
-assert_checkequal(sum(bs, 2, "native"), sparse([%t;%t]));
-assert_checkequal(sum(bs, 3, "native"),bs);
-// TODO : test the "m" option
index a1b453a..977d802 100644 (file)
@@ -7,6 +7,7 @@
 // =============================================================================
 
 // <-- CLI SHELL MODE -->
+// <-- NO CHECK REF -->
 // Check error
 d=[1 10;254 9];
 
@@ -201,7 +202,7 @@ for typ=T
     assert_checkequal(sum(i, "*", typ(:)), uint8(274));
     assert_checkequal(sum(i, 1, typ(:)), uint8([255 19]));
     assert_checkequal(sum(i, 2, typ(:)), uint8([11;263]));
-    assert_checkequal(sum(i, 3, typ(:)), double(i));
+    assert_checkequal(sum(i, 3, typ(:)), i);
 end
 
 assert_checkequal(sum(i, "double"), 274);
@@ -219,7 +220,7 @@ for typ=T
     assert_checkequal(sum(i, 1, typ(:)), matrix(uint8([255;19;1;0]), [1,2,2]));
     assert_checkequal(sum(i, 2, typ(:)), matrix(uint8([11;263;1;0]), [2,1,2]));
     assert_checkequal(sum(i, 3, typ(:)), uint8([2,10;254,9]));
-    assert_checkequal(sum(i, 5, typ(:)), double(i));
+    assert_checkequal(sum(i, 5, typ(:)), i);
 end
 
 assert_checkequal(sum(i, "double"), 275);