From 407b64ac1af2522793a3983703be01723e198dfb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Del=20Pino?= <stephane.delpino44@gmail.com>
Date: Mon, 19 Apr 2021 10:11:40 +0200
Subject: [PATCH] Add missing compatibility check when affecting lists to R^d
 or R^dxd

Add also a few missing tests
---
 .../ASTNodeAffectationExpressionBuilder.cpp   |  6 ++
 tests/test_AffectationProcessor.cpp           | 66 +++++++++++++++++++
 2 files changed, 72 insertions(+)

diff --git a/src/language/ast/ASTNodeAffectationExpressionBuilder.cpp b/src/language/ast/ASTNodeAffectationExpressionBuilder.cpp
index 321170395..cc26d368e 100644
--- a/src/language/ast/ASTNodeAffectationExpressionBuilder.cpp
+++ b/src/language/ast/ASTNodeAffectationExpressionBuilder.cpp
@@ -37,6 +37,12 @@ ASTNodeAffectationExpressionBuilder::ASTNodeAffectationExpressionBuilder(ASTNode
     ASTNode& lhs_node = *node.children[0];
     ASTNode& rhs_node = *node.children[1];
 
+    if (rhs_node.m_data_type == ASTNodeDataType::list_t) {
+      if ((lhs_node.m_data_type == ASTNodeDataType::vector_t) or (lhs_node.m_data_type == ASTNodeDataType::matrix_t)) {
+        ASTNodeNaturalConversionChecker(rhs_node, lhs_node.m_data_type);
+      }
+    }
+
     node.m_node_processor = optional_processor_builder.value()->getNodeProcessor(lhs_node, rhs_node);
   } else {
     std::ostringstream error_message;
diff --git a/tests/test_AffectationProcessor.cpp b/tests/test_AffectationProcessor.cpp
index ec130a135..b6f73afdb 100644
--- a/tests/test_AffectationProcessor.cpp
+++ b/tests/test_AffectationProcessor.cpp
@@ -482,6 +482,13 @@ TEST_CASE("AffectationProcessor", "[language]")
         CHECK_AFFECTATION_THROWS_WITH("let x : R^2, x = 3.2;", "undefined affectation type: R^2 = R");
         CHECK_AFFECTATION_THROWS_WITH("let x : R^3, x = 2.3;", "undefined affectation type: R^3 = R");
 
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^1; let y : R^1x1, y = 1; x = y;",
+                                      "undefined affectation type: R^1 = R^1x1");
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^2; let y : R^2x2, y = (1,2,4,4); x = y;",
+                                      "undefined affectation type: R^2 = R^2x2");
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^3; let y : R^3x3, y = (1,2,3,4,5,6,7,8,9); x = y;",
+                                      "undefined affectation type: R^3 = R^3x3");
+
         CHECK_AFFECTATION_THROWS_WITH("let x : R^2, x = 4;", "invalid integral value (0 is the solely valid value)");
         CHECK_AFFECTATION_THROWS_WITH("let x : R^3, x = 3;", "invalid integral value (0 is the solely valid value)");
 
@@ -499,6 +506,65 @@ TEST_CASE("AffectationProcessor", "[language]")
                                       "undefined affectation type: R^1 = R^3");
         CHECK_AFFECTATION_THROWS_WITH("let x : R^3, x = 0; let y : R^2, y = x;",
                                       "undefined affectation type: R^2 = R^3");
+
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^1, x = (1,2);", "undefined affectation type: R^1 = list");
+
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^2, x = (1,2,3);",
+                                      "incompatible dimensions in affectation: expecting 2, but provided 3");
+
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^3, x = (1,2);",
+                                      "incompatible dimensions in affectation: expecting 3, but provided 2");
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^3, x = (1,2,3,4);",
+                                      "incompatible dimensions in affectation: expecting 3, but provided 4");
+      }
+
+      SECTION("-> R^nxn")
+      {
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^2x2, x = \"foobar\";", "undefined affectation type: R^2x2 = string");
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^3x3, x = \"foobar\";", "undefined affectation type: R^3x3 = string");
+
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^2x2, x = 3.2;", "undefined affectation type: R^2x2 = R");
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^3x3, x = 2.3;", "undefined affectation type: R^3x3 = R");
+
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^1x1; let y : R^1, y = 1; x = y;",
+                                      "undefined affectation type: R^1x1 = R^1");
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^2x2; let y : R^2, y = (1,2); x = y;",
+                                      "undefined affectation type: R^2x2 = R^2");
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^3x3; let y : R^3, y = (1,2,3); x = y;",
+                                      "undefined affectation type: R^3x3 = R^3");
+
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^2x2; x = 3.2;", "undefined affectation type: R^2x2 = R");
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^3x3; x = 2.3;", "undefined affectation type: R^3x3 = R");
+
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^2x2, x = 4;", "invalid integral value (0 is the solely valid value)");
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^3x3, x = 3;", "invalid integral value (0 is the solely valid value)");
+
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^1x1, x = 0; let y : R^2x2, y = x;",
+                                      "undefined affectation type: R^2x2 = R^1x1");
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^1x1, x = 0; let y : R^3x3, y = x;",
+                                      "undefined affectation type: R^3x3 = R^1x1");
+
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^2x2, x = 0; let y : R^1x1, y = x;",
+                                      "undefined affectation type: R^1x1 = R^2x2");
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^2x2, x = 0; let y : R^3x3, y = x;",
+                                      "undefined affectation type: R^3x3 = R^2x2");
+
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^3x3, x = 0; let y : R^1x1, y = x;",
+                                      "undefined affectation type: R^1x1 = R^3x3");
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^3x3, x = 0; let y : R^2x2, y = x;",
+                                      "undefined affectation type: R^2x2 = R^3x3");
+
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^1x1, x = (1,2);", "undefined affectation type: R^1x1 = list");
+
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^2x2, x = (1,2,3);",
+                                      "incompatible dimensions in affectation: expecting 4, but provided 3");
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^2x2, x = (1,2,3,4,5);",
+                                      "incompatible dimensions in affectation: expecting 4, but provided 5");
+
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^3x3, x = (1,2,3,4,5,6,7,8);",
+                                      "incompatible dimensions in affectation: expecting 9, but provided 8");
+        CHECK_AFFECTATION_THROWS_WITH("let x : R^3x3, x = (1,2,3,4,5,6,7,8,9,10);",
+                                      "incompatible dimensions in affectation: expecting 9, but provided 10");
       }
     }
   }
-- 
GitLab