feat: print file and line number if `expect` fails

This makes debugging a failed expect easier.

Change-Id: I53a2b3cd1866c14f3983a2e36b94707a94f12ef3
Signed-off-by: Karl Meakin <karl.meakin@arm.com>
diff --git a/spm/cactus/cactus_main.c b/spm/cactus/cactus_main.c
index 141f1fe..b39f138 100644
--- a/spm/cactus/cactus_main.c
+++ b/spm/cactus/cactus_main.c
@@ -318,10 +318,10 @@
 
 	/* FFA_SECONDARY_EP_REGISTER interface is not supported for UP SP. */
 	if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
-		expect(ffa_func_id(ret), FFA_ERROR);
-		expect(ffa_error_code(ret), FFA_ERROR_NOT_SUPPORTED);
+		EXPECT(ffa_func_id(ret), FFA_ERROR);
+		EXPECT(ffa_error_code(ret), FFA_ERROR_NOT_SUPPORTED);
 	} else {
-		expect(ffa_func_id(ret), FFA_SUCCESS_SMC32);
+		EXPECT(ffa_func_id(ret), FFA_SUCCESS_SMC32);
 	}
 
 	discover_managed_exit_interrupt_id();
diff --git a/spm/cactus/cactus_tests/cactus_test_interrupts.c b/spm/cactus/cactus_tests/cactus_test_interrupts.c
index 2e0249c..c589119 100644
--- a/spm/cactus/cactus_tests/cactus_test_interrupts.c
+++ b/spm/cactus/cactus_tests/cactus_test_interrupts.c
@@ -36,7 +36,7 @@
 static void check_sec_wdog_interrupt_triggered(void)
 {
 	handle_sec_wdog_interrupt();
-	expect(flag_set, 0);
+	EXPECT(flag_set, 0);
 	flag_set = 1;
 }
 
@@ -233,7 +233,7 @@
 
 static void sec_interrupt_test_espi_handled(void)
 {
-	expect(test_espi_handled, false);
+	EXPECT(test_espi_handled, false);
 	test_espi_handled = true;
 	NOTICE("Interrupt handler for test espi interrupt called\n");
 
diff --git a/spm/cactus/cactus_tests/cactus_test_memory_sharing.c b/spm/cactus/cactus_tests/cactus_test_memory_sharing.c
index 15df77d..811c79f 100644
--- a/spm/cactus/cactus_tests/cactus_test_memory_sharing.c
+++ b/spm/cactus/cactus_tests/cactus_test_memory_sharing.c
@@ -122,7 +122,7 @@
 			: FFA_INSTRUCTION_ACCESS_NX,
 		0, NULL);
 
-	expect(memory_retrieve(mb, &m, handle, source, &receiver, 1,
+	EXPECT(memory_retrieve(mb, &m, handle, source, &receiver, 1,
 			       retrv_flags),
 	       true);
 
diff --git a/spm/cactus_mm/cactus_mm_tests_memory_attributes.c b/spm/cactus_mm/cactus_mm_tests_memory_attributes.c
index 1a87f6c..0a7af5f 100644
--- a/spm/cactus_mm/cactus_mm_tests_memory_attributes.c
+++ b/spm/cactus_mm/cactus_mm_tests_memory_attributes.c
@@ -105,12 +105,12 @@
 	new_attr = mem_access_perm(SP_MEMORY_ATTRIBUTES_NON_EXEC, SP_MEMORY_ATTRIBUTES_ACCESS_RW);
 
 	ret = request_mem_attr_changes(addr, pages_count, new_attr);
-	expect(ret, SPM_SUCCESS);
+	EXPECT(ret, SPM_SUCCESS);
 	printf("Successfully changed memory attributes\n");
 
 	/* The attributes should be the ones we have just written. */
 	ret = request_get_mem_attr(addr);
-	expect(ret, new_attr);
+	EXPECT(ret, new_attr);
 
 	/* If it worked, we should be able to write to this memory now! */
 	for (unsigned char *data = (unsigned char *) addr;
@@ -122,12 +122,12 @@
 
 	/* Let's revert back to the original attributes for the next test */
 	ret = request_mem_attr_changes(addr, pages_count, old_attr);
-	expect(ret, SPM_SUCCESS);
+	EXPECT(ret, SPM_SUCCESS);
 	printf("Successfully restored the old attributes\n");
 
 	/* The attributes should be the original ones again. */
 	ret = request_get_mem_attr(addr);
-	expect(ret, old_attr);
+	EXPECT(ret, old_attr);
 
 	announce_test_end(test_desc);
 }
@@ -157,7 +157,7 @@
 	announce_test_start(test_desc1);
 	attributes = mem_access_perm(SP_MEMORY_ATTRIBUTES_EXEC, SP_MEMORY_ATTRIBUTES_ACCESS_RW);
 	ret = request_mem_attr_changes(CACTUS_RWDATA_START, 1, attributes);
-	expect(ret, SPM_INVALID_PARAMETER);
+	EXPECT(ret, SPM_INVALID_PARAMETER);
 	announce_test_end(test_desc1);
 
 	const char *test_desc2 = "Size == 0";
@@ -165,7 +165,7 @@
 	announce_test_start(test_desc2);
 	attributes = mem_access_perm(SP_MEMORY_ATTRIBUTES_NON_EXEC, SP_MEMORY_ATTRIBUTES_ACCESS_RW);
 	ret = request_mem_attr_changes(CACTUS_RWDATA_START, 0, attributes);
-	expect(ret, SPM_INVALID_PARAMETER);
+	EXPECT(ret, SPM_INVALID_PARAMETER);
 	announce_test_end(test_desc2);
 
 	const char *test_desc3 = "Unaligned address";
@@ -175,7 +175,7 @@
 	/* Choose an address not aligned to a page boundary. */
 	addr = cactus_tests_start + 5;
 	ret = request_mem_attr_changes(addr, 1, attributes);
-	expect(ret, SPM_INVALID_PARAMETER);
+	EXPECT(ret, SPM_INVALID_PARAMETER);
 	announce_test_end(test_desc3);
 
 	const char *test_desc4 = "Unmapped memory region";
@@ -184,7 +184,7 @@
 	addr = boot_info->sp_mem_limit + 2 * PAGE_SIZE;
 	attributes = mem_access_perm(SP_MEMORY_ATTRIBUTES_NON_EXEC, SP_MEMORY_ATTRIBUTES_ACCESS_RW);
 	ret = request_mem_attr_changes(addr, 3, attributes);
-	expect(ret, SPM_INVALID_PARAMETER);
+	EXPECT(ret, SPM_INVALID_PARAMETER);
 	announce_test_end(test_desc4);
 
 	const char *test_desc5 = "Partially unmapped memory region";
@@ -193,7 +193,7 @@
 	addr = boot_info->sp_mem_base - 2 * PAGE_SIZE;
 	attributes = mem_access_perm(SP_MEMORY_ATTRIBUTES_NON_EXEC, SP_MEMORY_ATTRIBUTES_ACCESS_RW);
 	ret = request_mem_attr_changes(addr, 6, attributes);
-	expect(ret, SPM_INVALID_PARAMETER);
+	EXPECT(ret, SPM_INVALID_PARAMETER);
 	announce_test_end(test_desc5);
 
 	const char *test_desc6 = "Memory region mapped with the wrong granularity";
@@ -208,7 +208,7 @@
 	addr = ((uintptr_t)PLAT_ARM_UART_BASE + 0x200000ULL) & ~(0x200000ULL - 1ULL);
 	attributes = mem_access_perm(SP_MEMORY_ATTRIBUTES_NON_EXEC, SP_MEMORY_ATTRIBUTES_ACCESS_RW);
 	ret = request_mem_attr_changes(addr, 1, attributes);
-	expect(ret, SPM_INVALID_PARAMETER);
+	EXPECT(ret, SPM_INVALID_PARAMETER);
 	announce_test_end(test_desc6);
 
 	const char *test_desc7 = "Try some valid memory change requests";
diff --git a/spm/cactus_mm/cactus_mm_tests_misc.c b/spm/cactus_mm/cactus_mm_tests_misc.c
index 1d4d18a..83f2684 100644
--- a/spm/cactus_mm/cactus_mm_tests_misc.c
+++ b/spm/cactus_mm/cactus_mm_tests_misc.c
@@ -32,7 +32,7 @@
 	ret = sp_svc(&svc_values);
 	INFO("Version = 0x%x (%u.%u)\n", ret,
 	     (ret >> 16) & 0x7FFF, ret & 0xFFFF);
-	expect(ret, SPM_VERSION_COMPILED);
+	EXPECT(ret, SPM_VERSION_COMPILED);
 	announce_test_end(test_version);
 
 	announce_test_section_end(test_sect_desc);
diff --git a/spm/common/sp_helpers.c b/spm/common/sp_helpers.c
index eeb0d19..96a7d1e 100644
--- a/spm/common/sp_helpers.c
+++ b/spm/common/sp_helpers.c
@@ -30,15 +30,6 @@
  * Test framework helpers
  ******************************************************************************/
 
-void expect(int expr, int expected)
-{
-	if (expr != expected) {
-		ERROR("Expected value %i, got %i\n", expected, expr);
-		while (1)
-			continue;
-	}
-}
-
 void announce_test_section_start(const char *test_sect_desc)
 {
 	INFO("========================================\n");
diff --git a/spm/common/sp_helpers.h b/spm/common/sp_helpers.h
index a82924b..e0e749d 100644
--- a/spm/common/sp_helpers.h
+++ b/spm/common/sp_helpers.h
@@ -51,7 +51,20 @@
  * Check that expr == expected.
  * If not, loop forever.
  */
-void expect(int expr, int expected);
+#define EXPECT(lhs, rhs)                                                  \
+	do {                                                              \
+		int lhs_value = (lhs);                                    \
+		int rhs_value = (rhs);                                    \
+		if (lhs_value != rhs_value) {                             \
+			ERROR("%s:%d: Assertion failed: `%s == %s`\n",    \
+			      __FILE__, __LINE__, #lhs, #rhs);            \
+			ERROR("lhs = %d (0x%x)\n", lhs_value, lhs_value); \
+			ERROR("rhs = %d (0x%x)\n", rhs_value, rhs_value); \
+			while (1) {                                       \
+				continue;                                 \
+			}                                                 \
+		}                                                         \
+	} while (0)
 
 /*
  * Test framework functions
diff --git a/spm/common/sp_tests/sp_test_ffa.c b/spm/common/sp_tests/sp_test_ffa.c
index 9a32891..e8905a2 100644
--- a/spm/common/sp_tests/sp_test_ffa.c
+++ b/spm/common/sp_tests/sp_test_ffa.c
@@ -108,8 +108,8 @@
 
 	VERBOSE("%s: test request wrong UUID.\n", __func__);
 
-	expect(ffa_func_id(ret), FFA_ERROR);
-	expect(ffa_error_code(ret), FFA_ERROR_INVALID_PARAMETER);
+	EXPECT(ffa_func_id(ret), FFA_ERROR);
+	EXPECT(ffa_error_code(ret), FFA_ERROR_INVALID_PARAMETER);
 }
 
 static void ffa_partition_info_get_regs_test(void)
@@ -132,13 +132,13 @@
 		return;
 	}
 
-	expect(ffa_partition_info_regs_helper(sp_uuids[3],
+	EXPECT(ffa_partition_info_regs_helper(sp_uuids[3],
 		&ffa_expected_partition_info[3], 1), true);
-	expect(ffa_partition_info_regs_helper(sp_uuids[2],
+	EXPECT(ffa_partition_info_regs_helper(sp_uuids[2],
 		&ffa_expected_partition_info[2], 1), true);
-	expect(ffa_partition_info_regs_helper(sp_uuids[1],
+	EXPECT(ffa_partition_info_regs_helper(sp_uuids[1],
 		&ffa_expected_partition_info[1], 1), true);
-	expect(ffa_partition_info_regs_helper(sp_uuids[0],
+	EXPECT(ffa_partition_info_regs_helper(sp_uuids[0],
 		&ffa_expected_partition_info[0], 1), true);
 
 	/*
@@ -156,13 +156,13 @@
 	    (ffa_error_code(ret) == FFA_ERROR_INVALID_PARAMETER))) {
 		INFO("Skipping register based EL3 SPMD Logical partition"
 				" discovery\n");
-		expect(ffa_partition_info_regs_helper(NULL_UUID,
+		EXPECT(ffa_partition_info_regs_helper(NULL_UUID,
 			ffa_expected_partition_info,
 			(ARRAY_SIZE(ffa_expected_partition_info) - 1)), true);
 	} else {
-		expect(ffa_partition_info_regs_helper(sp_uuids[4],
+		EXPECT(ffa_partition_info_regs_helper(sp_uuids[4],
 			&ffa_expected_partition_info[4], 1), true);
-		expect(ffa_partition_info_regs_helper(NULL_UUID,
+		EXPECT(ffa_partition_info_regs_helper(NULL_UUID,
 			ffa_expected_partition_info,
 			ARRAY_SIZE(ffa_expected_partition_info)), true);
 	}
@@ -172,13 +172,13 @@
 {
 	INFO("Test FFA_PARTITION_INFO_GET.\n");
 
-	expect(ffa_partition_info_helper(mb, sp_uuids[2],
+	EXPECT(ffa_partition_info_helper(mb, sp_uuids[2],
 		&ffa_expected_partition_info[2], 1), true);
 
-	expect(ffa_partition_info_helper(mb, sp_uuids[1],
+	EXPECT(ffa_partition_info_helper(mb, sp_uuids[1],
 		&ffa_expected_partition_info[1], 1), true);
 
-	expect(ffa_partition_info_helper(mb, sp_uuids[0],
+	EXPECT(ffa_partition_info_helper(mb, sp_uuids[0],
 		&ffa_expected_partition_info[0], 1), true);
 
 	/*
@@ -188,7 +188,7 @@
 	 * uses the rx/tx buffer and the SPMD does not support the use of
 	 * rx/tx buffer to return SPMD logical partition information.
 	 */
-	expect(ffa_partition_info_helper(mb, NULL_UUID,
+	EXPECT(ffa_partition_info_helper(mb, NULL_UUID,
 		ffa_expected_partition_info,
 		(ARRAY_SIZE(ffa_expected_partition_info) - 1)), true);
 
@@ -211,7 +211,7 @@
 		spm_version & FFA_VERSION_MINOR_MASK,
 		(int)ffa_version_compatible);
 
-	expect((int)ffa_version_compatible, (int)true);
+	EXPECT((int)ffa_version_compatible, (int)true);
 }
 
 void ffa_spm_id_get_test(void)
@@ -219,7 +219,7 @@
 	if (spm_version >= MAKE_FFA_VERSION(1, 1)) {
 		struct ffa_value ret = ffa_spm_id_get();
 
-		expect(ffa_func_id(ret), FFA_SUCCESS_SMC32);
+		EXPECT(ffa_func_id(ret), FFA_SUCCESS_SMC32);
 
 		ffa_id_t spm_id = ffa_endpoint_id(ret);
 
@@ -229,7 +229,7 @@
 		 * Check the SPMC value given in the fvp_spmc_manifest
 		 * is returned.
 		 */
-		expect(spm_id, SPMC_ID);
+		EXPECT(spm_id, SPMC_ID);
 	} else {
 		INFO("FFA_SPM_ID_GET not supported in this version of FF-A."
 			" Test skipped.\n");