Repository: pyccel/pyccel (python)

Issue:
[SETS] Add C support for `clear()`
### Test Code
Provide code which does not currently work but which should do when this issue is fixed:
```python
def foo():
    a = {1,2,1}
    a.clear()
```

### Proposed Solution
This feature should be implemented using the functionalities provided by STC


Pull request description:
[SET] Add c support for `clear`
Add C and Fortran support for the `set` method `clear`. Fixes #1918 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3d04d0d3..5989f25c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -37,7 +37,8 @@ All notable changes to this project will be documented in this file.
 -   #1876 : Add C support for indexing lists.
 -   #1690 : Add C support for list method `pop()`.
 -   #1877 : Add C Support for set method `pop()`.
--   #1917 : Add C and Fortran Support for set method `add()`.
+-   #1917 : Add C and Fortran support for set method `add()`.
+-   #1918 : Add C and Fortran support for set method `clear()`.
 -   #1936 : Add missing C output for inline decorator example in documentation
 -   #1937 : Optimise `pyccel.ast.basic.PyccelAstNode.substitute` method.
 -   #1544 : Add support for `typing.TypeAlias`.
diff --git a/docs/builtin-functions.md b/docs/builtin-functions.md
index 9f3dcc1e..bfebd900 100644
--- a/docs/builtin-functions.md
+++ b/docs/builtin-functions.md
@@ -97,7 +97,7 @@ Python contains a limited number of builtin functions defined [here](https://doc
 | Method | Supported |
 |----------|-----------|
 | **`add`** | **Yes** |
-| `clear` | Python-only |
+| **`clear`** | **Yes** |
 | `copy` | Python-only |
 | `difference` | No |
 | `difference_update` | No |
@@ -107,7 +107,7 @@ Python contains a limited number of builtin functions defined [here](https://doc
 | `isdisjoint` | No |
 | `issubset` | No |
 | `issuperset` | No |
-| `pop` | Python-only |
+| `pop` | C and Python |
 | `remove` | Python-only |
 | `symmetric_difference` | No |
 | `symmetric_difference_update` | No |
diff --git a/pyccel/codegen/printing/ccode.py b/pyccel/codegen/printing/ccode.py
index 60437513..93e1ead8 100644
--- a/pyccel/codegen/printing/ccode.py
+++ b/pyccel/codegen/printing/ccode.py
@@ -2617,6 +2617,11 @@ class CCodePrinter(CodePrinter):
         set_var = self._print(ObjectAddress(expr.set_variable))
         return f'{var_type}_pop({set_var})'
 
+    def _print_SetClear(self, expr):
+        var_type = self.get_declare_type(expr.set_variable)
+        set_var = self._print(ObjectAddress(expr.set_variable))
+        return f'{var_type}_clear({set_var});\n'
+
     #=================== MACROS ==================
 
     def _print_MacroShape(self, expr):
diff --git a/pyccel/codegen/printing/fcode.py b/pyccel/codegen/printing/fcode.py
index 706157a3..ca5c3de8 100644
--- a/pyccel/codegen/printing/fcode.py
+++ b/pyccel/codegen/printing/fcode.py
@@ -1242,7 +1242,6 @@ class FCodePrinter(CodePrinter):
         arg = self._print(expr.args[0])
         return f'call {target} % push_back({arg})\n'
 
-
     #========================== Set Methods ================================#
 
     def _print_SetAdd(self, expr):
@@ -1250,6 +1249,10 @@ class FCodePrinter(CodePrinter):
         insert_obj = self._print(expr.args[0])
         return f'call {var} % insert( {insert_obj} )\n'
 
+    def _print_SetClear(self, expr):
+        var = self._print(expr.set_variable)
+        return f'call {var} % clear()\n'
+
     #========================== Numpy Elements ===============================#
 
     def _print_NumpySum(self, expr):


Test patch:
diff --git a/tests/epyccel/test_epyccel_sets.py b/tests/epyccel/test_epyccel_sets.py
index 455fd1a3..72b7bf78 100644
--- a/tests/epyccel/test_epyccel_sets.py
+++ b/tests/epyccel/test_epyccel_sets.py
@@ -47,32 +47,32 @@ def test_add_variable_int(language):
     python_result = add_element_range()
     assert python_result == pyccel_result
 
-def test_clear_int(python_only_language):
+def test_clear_int(language):
     def clear_int():
         se = {1,2,4,5}
         se.clear()
-        return se
-    epyccel_clear = epyccel(clear_int, language = python_only_language)
+        return len(se)
+    epyccel_clear = epyccel(clear_int, language = language)
     pyccel_result = epyccel_clear()
     python_result = clear_int()
     assert python_result == pyccel_result
 
-def test_clear_float(python_only_language):
+def test_clear_float(language):
     def clear_float():
         se = {7.2, 2.1, 9.8, 6.4}
         se.clear()
-        return se
-    epyccel_clear = epyccel(clear_float, language = python_only_language)
+        return len(se)
+    epyccel_clear = epyccel(clear_float, language = language)
     pyccel_result = epyccel_clear()
     python_result = clear_float()
     assert python_result == pyccel_result
 
-def test_clear_complex(python_only_language):
+def test_clear_complex(language):
     def clear_complex():
         se = {3j, 6j, 2j}
         se.clear()
-        return se
-    epyccel_clear = epyccel(clear_complex, language = python_only_language)
+        return len(se)
+    epyccel_clear = epyccel(clear_complex, language = language)
     pyccel_result = epyccel_clear()
     python_result = clear_complex()
     assert python_result == pyccel_result