From a32892926c69ea0bad031fd9d7bfc915cc5e9b68 Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Thu, 12 Mar 2020 12:47:25 +0100 Subject: [PATCH] Matrix: capture stdout for print() test I want to switch the print() function back to use printf() such that it's fully compatible with all (embedded) platforms. To still test the print function I'm capturing stdout into a file such that the print() function can stay unchanged and the result of the printf()s can be evaluated. --- matrix/Matrix.hpp | 4 ++-- test/matrixAssignment.cpp | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/matrix/Matrix.hpp b/matrix/Matrix.hpp index 4af752bb3e..93077a8182 100644 --- a/matrix/Matrix.hpp +++ b/matrix/Matrix.hpp @@ -350,13 +350,13 @@ public: } } - void print(FILE *stream = stdout) const + void print() const { // element: tab, point, 8 digits, 4 scientific notation chars; row: newline; string: \0 end static const size_t n = 15*N*M + M + 1; char * buf = new char[n]; write_string(buf, n); - fprintf(stream, "%s\n", buf); + printf("%s\n", buf); delete[] buf; } diff --git a/test/matrixAssignment.cpp b/test/matrixAssignment.cpp index 42523f4ca2..052b8d6cc8 100644 --- a/test/matrixAssignment.cpp +++ b/test/matrixAssignment.cpp @@ -235,13 +235,13 @@ int main() } // check print() + // Redirect stdout + TEST(freopen("testoutput.txt", "w", stdout) != NULL); // write - FILE *fp = fopen("testoutput.txt", "w+"); - TEST(fp != nullptr); - Comma.print(fp); - TEST(!fclose(fp)); + Comma.print(); + fclose(stdout); // read - fp = fopen("testoutput.txt", "r"); + FILE *fp = fopen("testoutput.txt", "r"); TEST(fp != nullptr); TEST(!fseek(fp, 0, SEEK_SET)); for (size_t i = 0; i < len; i++) { @@ -249,7 +249,7 @@ int main() if (c == '\n') { break; } - printf("%d %d %c\n", static_cast(i), c, c); + printf("%d %d %d\n", static_cast(i), output[i], c); TEST(c == output[i]); } TEST(!fclose(fp));