Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3307356

Browse filesBrowse files
Added unit test for virtual destructors
The purpose of this tests is to hihglight the need for a virtual destructor through valgrind unit test execution
1 parent e97e5e2 commit 3307356
Copy full SHA for 3307356

File tree

5 files changed

+161
-0
lines changed
Filter options

5 files changed

+161
-0
lines changed

‎test/CMakeLists.txt

Copy file name to clipboardExpand all lines: test/CMakeLists.txt
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ set(TEST_SRCS
112112
src/WCharacter/test_isUpperCase.cpp
113113
src/WCharacter/test_isWhitespace.cpp
114114
src/WCharacter/test_toAscii.cpp
115+
src/Interfaces/test_virtualDestructor.cpp
115116
)
116117

117118
set(TEST_DUT_SRCS

‎test/include/TCPClientMock.h

Copy file name to clipboard
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2020 Arduino. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: LGPL-2.1-or-later
5+
*/
6+
7+
#ifndef TCPCLIENT_MOCK_H_
8+
#define TCPCLIENT_MOCK_H_
9+
10+
/**************************************************************************************
11+
* INCLUDE
12+
**************************************************************************************/
13+
14+
#include <api/Client.h>
15+
16+
/**************************************************************************************
17+
* CLASS DECLARATION
18+
**************************************************************************************/
19+
20+
/*
21+
* The purpose of this class is currently to highlight the effects of lacking virtual destructor
22+
*/
23+
24+
#pragma GCC diagnostic push
25+
#pragma GCC diagnostic ignored "-Wunused-parameter"
26+
27+
class TCPClientMock : public Client {
28+
public:
29+
virtual int connect(IPAddress ip, uint16_t port) { return 0; }
30+
virtual int connect(const char *host, uint16_t port) { return 0; }
31+
virtual size_t write(uint8_t) { return 0; }
32+
virtual size_t write(const uint8_t *buf, size_t size) { return 0;}
33+
virtual int available() { return 0; }
34+
virtual int read() { return 0; }
35+
virtual int read(uint8_t *buf, size_t size) { return 0;}
36+
virtual int peek() { return 0; }
37+
virtual void flush() {}
38+
virtual void stop() {}
39+
virtual uint8_t connected() { return 0;}
40+
virtual operator bool() { return true; }
41+
};
42+
#pragma GCC diagnostic pop
43+
44+
45+
#endif /* TCPCLIENT_MOCK_H_ */
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2020 Arduino. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: LGPL-2.1-or-later
5+
*/
6+
7+
#include <catch2/catch_test_macros.hpp>
8+
#include <api/IPAddress.h>
9+
#include <TCPClientMock.h>
10+
11+
/*
12+
* The purpose of these tests is to highlight potential memory leaking
13+
* issues that may arise from the lack of virtual destructors.
14+
* These test cases will never fail under unit testing,
15+
* but they should trigger valgrind error reporting
16+
*/
17+
18+
TEST_CASE("Testing polymorphic IPAddress memory free", "[ipaddress-delete-01]")
19+
{
20+
arduino::Printable* p = new IPAddress();
21+
22+
#pragma GCC diagnostic push
23+
#pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
24+
delete p;
25+
#pragma GCC diagnostic pop
26+
}
27+
28+
TEST_CASE("Testing polymorphic client memory free", "[client-delete-01]")
29+
{
30+
arduino::Client* p = new TCPClientMock;
31+
32+
#pragma GCC diagnostic push
33+
#pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
34+
delete p;
35+
#pragma GCC diagnostic pop
36+
}
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
FQBNS=(
4+
arduino-git:renesas_portenta_main:unor4wifi
5+
# arduino-git:mbed:envie_m7
6+
# arduino-git:avr:uno
7+
# arduino-git:samd:mkrwifi1010
8+
# arduino-git:mbed:opta
9+
)
10+
11+
branches=(
12+
master
13+
virtual-destructors
14+
)
15+
16+
for branch in "${branches[@]}"; do
17+
git checkout $branch
18+
for fqbn in $"${FQBNS[@]}"; do
19+
echo "compiling for " $fqbn.$branch
20+
# arduino-cli compile -b $fqbn | tee $fqbn.$branch.log
21+
arduino-cli compile -b $fqbn --build-property "compiler.cpp.extra_flags=\"-fdump-record-layouts\""
22+
# /home/agilardoni/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/bin/arm-none-eabi-size -A /tmp/arduino/sketches/97A7C8915D2115841AF9E1023FFD9539/virtual_destructors_testing.ino.elf | tee $fqbn.$branch.size.log
23+
done
24+
done
+55Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
#if defined(ARDUINO_SAMD_MKR1000)
3+
#include <WiFi101.h>
4+
#include <WiFiUdp.h>
5+
#elif defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined (ARDUINO_NANO_RP2040_CONNECT)
6+
#include <WiFiNINA.h>
7+
#include <WiFiUdp.h>
8+
#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M7) || \
9+
defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA)
10+
#include <WiFi.h>
11+
#include <WiFiUdp.h>
12+
#elif defined(ARDUINO_PORTENTA_C33)
13+
#include <WiFiC3.h>
14+
#include <WiFiUdp.h>
15+
#elif defined(ARDUINO_ARCH_ESP8266)
16+
#include <ESP8266WiFi.h>
17+
#include <WiFiUdp.h>
18+
#elif defined(ARDUINO_ARCH_ESP32)
19+
#include <WiFi.h>
20+
#include <WiFiUdp.h>
21+
#elif defined(ARDUINO_UNOR4_WIFI)
22+
#include <WiFiS3.h>
23+
#elif defined(ARDUINO_RASPBERRY_PI_PICO_W)
24+
#include <WiFi.h>
25+
#include <WiFiUdp.h>
26+
#endif
27+
void setup() {
28+
Serial.begin(115200);
29+
30+
while(!Serial);
31+
32+
Serial.println("Hello world");
33+
34+
#if defined(ARDUINO_SAMD_MKR1000) || \
35+
defined(ARDUINO_SAMD_MKRWIFI1010) || \
36+
defined(ARDUINO_SAMD_NANO_33_IOT) || \
37+
defined(ARDUINO_AVR_UNO_WIFI_REV2) || \
38+
defined(ARDUINO_NANO_RP2040_CONNECT) || \
39+
defined(ARDUINO_PORTENTA_H7_M7) || \
40+
defined(ARDUINO_PORTENTA_H7_M7) || \
41+
defined(ARDUINO_NICLA_VISION) || \
42+
defined(ARDUINO_OPTA) || \
43+
defined(ARDUINO_GIGA) || \
44+
defined(ARDUINO_PORTENTA_C33) || \
45+
defined(ARDUINO_ARCH_ESP8266) || \
46+
defined(ARDUINO_ARCH_ESP32) || \
47+
defined(ARDUINO_UNOR4_WIFI) || \
48+
defined(ARDUINO_RASPBERRY_PI_PICO_W)
49+
Client *c = new WiFiClient(); // sizeof // as global variable
50+
delete c;
51+
#endif
52+
}
53+
54+
void loop() {
55+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.