From 1624025f4764d568a47dde45a2b4bc7510431c2f Mon Sep 17 00:00:00 2001
From: David Hendriks <davidhendriks93@gmail.com>
Date: Wed, 19 Aug 2020 13:19:12 +0100
Subject: [PATCH] added some tests for the persistent data and modified the
 bindings

---
 src/binary_c_python.c                        |  3 +-
 tests/test_return_persistent_data_memaddr.py | 62 ++++++++++++++++----
 2 files changed, 52 insertions(+), 13 deletions(-)

diff --git a/src/binary_c_python.c b/src/binary_c_python.c
index 48e0320f4..ef19fdfd0 100644
--- a/src/binary_c_python.c
+++ b/src/binary_c_python.c
@@ -238,7 +238,7 @@ static PyObject* binary_c_run_system(PyObject *self, PyObject *args, PyObject *k
         return NULL;
     }
 
-    printf("Input persistent_Data_memaddr: %lu", persistent_data_memaddr);
+    printf("Input persistent_Data_memaddr: %lu\n", persistent_data_memaddr);
 
     /* Call c-function */
     char * buffer;
@@ -270,7 +270,6 @@ static PyObject* binary_c_run_system(PyObject *self, PyObject *args, PyObject *k
     Safe_free(error_buffer);
 
     // TODO: fix that the return_error_string is returned.
-
     return return_string;
 }
 
diff --git a/tests/test_return_persistent_data_memaddr.py b/tests/test_return_persistent_data_memaddr.py
index d5551ff72..cf0db984e 100644
--- a/tests/test_return_persistent_data_memaddr.py
+++ b/tests/test_return_persistent_data_memaddr.py
@@ -1,5 +1,6 @@
 import binary_c_python_api
 import textwrap
+import json
 
 
 # Evolution functions
@@ -10,17 +11,19 @@ def test_return_persistent_data_memaddr():
     print("Binary_c output:")
     print(textwrap.indent(str(output), "\t"))
 
-    return output
-
 def test_passing_persistent_data_to_run_system():
+    # Function to test the passing of the persistent data memoery adress, and having ensemble_defer = True
+    # We should see that the results of multiple systems have been added to the one output json
+
     m1 = 15.0  # Msun
     m2 = 14.0  # Msun
     separation = 0  # 0 = ignored, use period
-    orbital_period = 4530.0  # days
+    orbital_period = 453000000000.0  # days
     eccentricity = 0.0
     metallicity = 0.02
     max_evolution_time = 15000
-    argstring = "binary_c M_1 {0:g} M_2 {1:g} separation {2:g} orbital_period {3:g} eccentricity {4:g} metallicity {5:g} max_evolution_time {6:g}  ".format(
+
+    argstring_1 = "binary_c M_1 {0:g} M_2 {1:g} separation {2:g} orbital_period {3:g} eccentricity {4:g} metallicity {5:g} max_evolution_time {6:g} ensemble 1 ensemble_filters_off 1 ensemble_filter_SUPERNOVAE 1".format(
         m1,
         m2,
         separation,
@@ -30,17 +33,54 @@ def test_passing_persistent_data_to_run_system():
         max_evolution_time,
     )
 
-    persistent_data_memaddr = test_return_persistent_data_memaddr()
-    print(persistent_data_memaddr)
-    output = binary_c_python_api.run_system(argstring=argstring, persistent_data_memaddr=persistent_data_memaddr)
+    argstring_1_deferred = "binary_c M_1 {0:g} M_2 {1:g} separation {2:g} orbital_period {3:g} eccentricity {4:g} metallicity {5:g} max_evolution_time {6:g} ensemble 1 ensemble_defer 1 ensemble_filters_off 1 ensemble_filter_SUPERNOVAE 1".format(
+        m1,
+        m2,
+        separation,
+        orbital_period,
+        eccentricity,
+        metallicity,
+        max_evolution_time,
+    )
+    argstring2 = "binary_c M_1 {0:g} M_2 {1:g} separation {2:g} orbital_period {3:g} eccentricity {4:g} metallicity {5:g} max_evolution_time {6:g} ensemble 1 ensemble_filters_off 1 ensemble_filter_SUPERNOVAE 1".format(
+        m1+10,
+        m2,
+        separation,
+        orbital_period,
+        eccentricity,
+        metallicity,
+        max_evolution_time,
+    )
+
+    persistent_data_memaddr = binary_c_python_api.return_persistent_data_memaddr("")
+    # print(persistent_data_memaddr)
 
-    print("function: test_run_system")
-    print("Binary_c output:")
-    print(textwrap.indent(output, "\t"))
+    output_1 = binary_c_python_api.run_system(argstring=argstring_1)
+    ensemble_jsons_1 = [line for line in output_1.splitlines() if line.startswith("ENSEMBLE_JSON")]
+    json_1 = json.loads(ensemble_jsons_1[0][len("ENSEMBLE_JSON "):])
+
+    # Doing 2 systems in a row.
+    output_1_deferred = binary_c_python_api.run_system(argstring=argstring_1_deferred, persistent_data_memaddr=persistent_data_memaddr)
+    output_2 = binary_c_python_api.run_system(argstring=argstring2, persistent_data_memaddr=persistent_data_memaddr)
+    ensemble_jsons_2 = [line for line in output_2.splitlines() if line.startswith("ENSEMBLE_JSON")]
+    json_2 = json.loads(ensemble_jsons_2[0][len("ENSEMBLE_JSON "):])
+
+    # Doing system one again.
+    output_1_again = binary_c_python_api.run_system(argstring=argstring_1)
+    ensemble_jsons_1 = [line for line in output_1_again.splitlines() if line.startswith("ENSEMBLE_JSON")]
+    json_1_again = json.loads(ensemble_jsons_1[0][len("ENSEMBLE_JSON "):])
+
+    print("Twice the same system are they equal?:")
+    print(json_1 == json_1_again)
+
+    print("first system vs deferred double system?:")
+    print(json_1 == json_2)
+
+    # printf("combined double system vs deferred double system?:")
 
 
 
 ####
 if __name__ == "__main__":
     test_return_persistent_data_memaddr()
-    test_passing_persistent_data_to_run_system()
\ No newline at end of file
+    test_passing_persistent_data_to_run_system()
-- 
GitLab