Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1066)

Side by Side Diff: runtime/vm/log_test.cc

Issue 2651013002: Fix memory leaks in cc/Log_* tests (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/tests/vm/vm.status ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/globals.h" 5 #include "platform/globals.h"
6 6
7 #include "include/dart_tools_api.h" 7 #include "include/dart_tools_api.h"
8 #include "vm/dart_api_impl.h" 8 #include "vm/dart_api_impl.h"
9 #include "vm/dart_entry.h" 9 #include "vm/dart_entry.h"
10 #include "vm/debugger.h" 10 #include "vm/debugger.h"
11 #include "vm/globals.h" 11 #include "vm/globals.h"
12 #include "vm/isolate.h" 12 #include "vm/isolate.h"
13 #include "vm/log.h" 13 #include "vm/log.h"
14 #include "vm/message_handler.h" 14 #include "vm/message_handler.h"
15 #include "vm/unit_test.h" 15 #include "vm/unit_test.h"
16 16
17 namespace dart { 17 namespace dart {
18 18
19 static const char* test_output_; 19 static const char* test_output_ = NULL;
20 static void TestPrinter(const char* format, ...) { 20 static void TestPrinter(const char* format, ...) {
21 // Measure. 21 // Measure.
22 va_list args; 22 va_list args;
23 va_start(args, format); 23 va_start(args, format);
24 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 24 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
25 va_end(args); 25 va_end(args);
26 26
27 // Print string to buffer. 27 // Print string to buffer.
28 char* buffer = reinterpret_cast<char*>(malloc(len + 1)); 28 char* buffer = reinterpret_cast<char*>(malloc(len + 1));
29 va_list args2; 29 va_list args2;
30 va_start(args2, format); 30 va_start(args2, format);
31 OS::VSNPrint(buffer, (len + 1), format, args2); 31 OS::VSNPrint(buffer, (len + 1), format, args2);
32 va_end(args2); 32 va_end(args2);
33 33
34 // Leaks buffer. 34 if (test_output_ != NULL) {
35 free(const_cast<char*>(test_output_));
36 test_output_ = NULL;
37 }
35 test_output_ = buffer; 38 test_output_ = buffer;
36 } 39 }
37 40
38 class LogTestHelper : public AllStatic { 41 class LogTestHelper : public AllStatic {
39 public: 42 public:
40 static void SetPrinter(Log* log, LogPrinter printer) { 43 static void SetPrinter(Log* log, LogPrinter printer) {
41 ASSERT(log != NULL); 44 ASSERT(log != NULL);
42 ASSERT(printer != NULL); 45 ASSERT(printer != NULL);
43 log->printer_ = printer; 46 log->printer_ = printer;
44 } 47 }
48
49 static void FreeTestOutput() {
50 if (test_output_ != NULL) {
Vyacheslav Egorov (Google) 2017/01/24 16:10:51 No need to check if it is not NULL - free(NULL) is
51 free(const_cast<char*>(test_output_));
52 test_output_ = NULL;
53 }
54 }
45 }; 55 };
46 56
47 57
48 TEST_CASE(Log_Macro) { 58 TEST_CASE(Log_Macro) {
49 test_output_ = NULL; 59 test_output_ = NULL;
50 Log* log = Log::Current(); 60 Log* log = Log::Current();
51 LogTestHelper::SetPrinter(log, TestPrinter); 61 LogTestHelper::SetPrinter(log, TestPrinter);
52 62
53 THR_Print("Hello %s", "World"); 63 THR_Print("Hello %s", "World");
54 EXPECT_STREQ("Hello World", test_output_); 64 EXPECT_STREQ("Hello World", test_output_);
55 THR_Print("SingleArgument"); 65 THR_Print("SingleArgument");
56 EXPECT_STREQ("SingleArgument", test_output_); 66 EXPECT_STREQ("SingleArgument", test_output_);
67 LogTestHelper::FreeTestOutput();
57 } 68 }
58 69
59 70
60 TEST_CASE(Log_Basic) { 71 TEST_CASE(Log_Basic) {
61 test_output_ = NULL; 72 test_output_ = NULL;
Vyacheslav Egorov (Google) 2017/01/24 16:10:51 Some sort of scoped object might be a better way t
62 Log* log = new Log(TestPrinter); 73 Log* log = new Log(TestPrinter);
63 74
64 EXPECT_EQ(reinterpret_cast<const char*>(NULL), test_output_); 75 EXPECT_EQ(reinterpret_cast<const char*>(NULL), test_output_);
65 log->Print("Hello %s", "World"); 76 log->Print("Hello %s", "World");
66 EXPECT_STREQ("Hello World", test_output_); 77 EXPECT_STREQ("Hello World", test_output_);
78
79 delete log;
80 LogTestHelper::FreeTestOutput();
67 } 81 }
68 82
69 83
70 TEST_CASE(Log_Block) { 84 TEST_CASE(Log_Block) {
71 test_output_ = NULL; 85 test_output_ = NULL;
72 Log* log = new Log(TestPrinter); 86 Log* log = new Log(TestPrinter);
73 87
74 EXPECT_EQ(reinterpret_cast<const char*>(NULL), test_output_); 88 EXPECT_EQ(reinterpret_cast<const char*>(NULL), test_output_);
75 { 89 {
76 LogBlock ba(thread, log); 90 LogBlock ba(thread, log);
77 log->Print("APPLE"); 91 log->Print("APPLE");
78 EXPECT_EQ(reinterpret_cast<const char*>(NULL), test_output_); 92 EXPECT_EQ(reinterpret_cast<const char*>(NULL), test_output_);
79 { 93 {
80 LogBlock ba(thread, log); 94 LogBlock ba(thread, log);
81 log->Print("BANANA"); 95 log->Print("BANANA");
82 EXPECT_EQ(reinterpret_cast<const char*>(NULL), test_output_); 96 EXPECT_EQ(reinterpret_cast<const char*>(NULL), test_output_);
83 } 97 }
84 EXPECT_STREQ("BANANA", test_output_); 98 EXPECT_STREQ("BANANA", test_output_);
85 } 99 }
86 EXPECT_STREQ("APPLE", test_output_); 100 EXPECT_STREQ("APPLE", test_output_);
101 delete log;
102 LogTestHelper::FreeTestOutput();
87 } 103 }
88 104
89 } // namespace dart 105 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/tests/vm/vm.status ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698