Skip to content

Commit 105276f

Browse files
authored
Merge pull request #122 from arduino/ci-unit-test
Adding CI unit testing for ArduinoCore-API
2 parents 76e931e + c4a350a commit 105276f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+21567
-31
lines changed

Diff for: .github/workflows/unit-tests.yml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Unit Tests
2+
3+
on:
4+
pull_request:
5+
# Only run workflow if a file in these paths are modified
6+
paths:
7+
- ".github/workflows/unit-tests.yml"
8+
- "test/**"
9+
- "api/**"
10+
11+
push:
12+
paths:
13+
- ".github/workflows/unit-tests.yml"
14+
- "test/**"
15+
- "api/**"
16+
17+
jobs:
18+
test:
19+
name: Run unit tests
20+
runs-on: ubuntu-latest
21+
22+
env:
23+
COVERAGE_DATA_PATH: extras/coverage-data/coverage.info
24+
25+
steps:
26+
- name: Checkout repository
27+
uses: actions/checkout@v2
28+
29+
# See: https://github.com/arduino/cpp-test-action/blob/main/README.md
30+
- uses: arduino/cpp-test-action@main
31+
with:
32+
source-path: test
33+
build-path: test/build
34+
runtime-path: test/build/bin/test-ArduinoCore-API
35+
coverage-exclude-paths: |
36+
- '*/test/*'
37+
- '/usr/*'
38+
coverage-data-path: ${{ env.COVERAGE_DATA_PATH }}
39+
40+
# See: https://github.com/codecov/codecov-action/blob/master/README.md
41+
- name: Upload coverage report to Codecov
42+
uses: codecov/codecov-action@v1
43+
with:
44+
file: ${{ env.COVERAGE_DATA_PATH }}
45+
fail_ci_if_error: true

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ArduinoCore-API
22

3+
[![Unit Tests](https://github.com/arduino/ArduinoCore-API/workflows/Unit%20Tests/badge.svg)](https://github.com/arduino/ArduinoCore-API/actions?workflow=Unit+Tests)
4+
[![codecov](https://codecov.io/gh/arduino/ArduinoCore-API/branch/master/graph/badge.svg)](https://codecov.io/gh/arduino/ArduinoCore-API)
5+
36
This repository hosts the hardware independent layer of Arduino core.
47

58
All Arduino official cores are being ported to the new structure so they take advantage of this single repo.

Diff for: api/Common.h

+2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ typedef uint16_t word;
7979
void init(void);
8080
void initVariant(void);
8181

82+
#ifndef HOST
8283
int atexit(void (*func)()) __attribute__((weak));
84+
#endif
8385
int main() __attribute__((weak));
8486

8587
#ifdef EXTENDED_PIN_MODE

Diff for: api/IPAddress.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ bool IPAddress::fromString(const char *address)
4949
{
5050
// TODO: add support for "a", "a.b", "a.b.c" formats
5151

52-
uint16_t acc = 0; // Accumulator
52+
int16_t acc = -1; // Accumulator
5353
uint8_t dots = 0;
5454

5555
while (*address)
5656
{
5757
char c = *address++;
5858
if (c >= '0' && c <= '9')
5959
{
60-
acc = acc * 10 + (c - '0');
60+
acc = (acc < 0) ? (c - '0') : acc * 10 + (c - '0');
6161
if (acc > 255) {
6262
// Value out of [0..255] range
6363
return false;
@@ -69,8 +69,12 @@ bool IPAddress::fromString(const char *address)
6969
// Too much dots (there must be 3 dots)
7070
return false;
7171
}
72+
if (acc < 0) {
73+
/* No value between dots, e.g. '1..' */
74+
return false;
75+
}
7276
_address.bytes[dots++] = acc;
73-
acc = 0;
77+
acc = -1;
7478
}
7579
else
7680
{
@@ -83,6 +87,10 @@ bool IPAddress::fromString(const char *address)
8387
// Too few dots (there must be 3 dots)
8488
return false;
8589
}
90+
if (acc < 0) {
91+
/* No value between dots, e.g. '1..' */
92+
return false;
93+
}
8694
_address.bytes[3] = acc;
8795
return true;
8896
}

Diff for: api/Print.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,11 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
332332
return bytes;
333333
}
334334

335-
size_t Print::printFloat(double number, uint8_t digits)
335+
size_t Print::printFloat(double number, int digits)
336336
{
337+
if (digits < 0)
338+
digits = 2;
339+
337340
size_t n = 0;
338341

339342
if (isnan(number)) return print("nan");

Diff for: api/Print.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Print
3737
int write_error;
3838
size_t printNumber(unsigned long, uint8_t);
3939
size_t printULLNumber(unsigned long long, uint8_t);
40-
size_t printFloat(double, uint8_t);
40+
size_t printFloat(double, int);
4141
protected:
4242
void setWriteError(int err = 1) { write_error = err; }
4343
public:

Diff for: api/RingBuffer.h

+13-17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#define _RING_BUFFER_
2323

2424
#include <stdint.h>
25+
#include <string.h>
2526

2627
namespace arduino {
2728

@@ -38,6 +39,7 @@ class RingBufferN
3839
uint8_t _aucBuffer[N] ;
3940
volatile int _iHead ;
4041
volatile int _iTail ;
42+
volatile int _numElems;
4143

4244
public:
4345
RingBufferN( void ) ;
@@ -51,6 +53,7 @@ class RingBufferN
5153

5254
private:
5355
int nextIndex(int index);
56+
inline bool isEmpty() const { return (_numElems == 0); }
5457
};
5558

5659
typedef RingBufferN<SERIAL_BUFFER_SIZE> RingBuffer;
@@ -66,16 +69,15 @@ RingBufferN<N>::RingBufferN( void )
6669
template <int N>
6770
void RingBufferN<N>::store_char( uint8_t c )
6871
{
69-
int i = nextIndex(_iHead);
70-
7172
// if we should be storing the received character into the location
7273
// just before the tail (meaning that the head would advance to the
7374
// current location of the tail), we're about to overflow the buffer
7475
// and so we don't write the character or advance the head.
75-
if ( i != _iTail )
76+
if (!isFull())
7677
{
7778
_aucBuffer[_iHead] = c ;
78-
_iHead = i ;
79+
_iHead = nextIndex(_iHead);
80+
_numElems++;
7981
}
8082
}
8183

@@ -84,44 +86,38 @@ void RingBufferN<N>::clear()
8486
{
8587
_iHead = 0;
8688
_iTail = 0;
89+
_numElems = 0;
8790
}
8891

8992
template <int N>
9093
int RingBufferN<N>::read_char()
9194
{
92-
if(_iTail == _iHead)
95+
if (isEmpty())
9396
return -1;
9497

9598
uint8_t value = _aucBuffer[_iTail];
9699
_iTail = nextIndex(_iTail);
100+
_numElems--;
97101

98102
return value;
99103
}
100104

101105
template <int N>
102106
int RingBufferN<N>::available()
103107
{
104-
int delta = _iHead - _iTail;
105-
106-
if(delta < 0)
107-
return N + delta;
108-
else
109-
return delta;
108+
return _numElems;
110109
}
111110

112111
template <int N>
113112
int RingBufferN<N>::availableForStore()
114113
{
115-
if (_iHead >= _iTail)
116-
return N - 1 - _iHead + _iTail;
117-
else
118-
return _iTail - _iHead - 1;
114+
return (N - _numElems);
119115
}
120116

121117
template <int N>
122118
int RingBufferN<N>::peek()
123119
{
124-
if(_iTail == _iHead)
120+
if (isEmpty())
125121
return -1;
126122

127123
return _aucBuffer[_iTail];
@@ -136,7 +132,7 @@ int RingBufferN<N>::nextIndex(int index)
136132
template <int N>
137133
bool RingBufferN<N>::isFull()
138134
{
139-
return (nextIndex(_iHead) == _iTail);
135+
return (_numElems == N);
140136
}
141137

142138
}

Diff for: api/String.cpp

+13-8
Original file line numberDiff line numberDiff line change
@@ -646,17 +646,22 @@ void String::replace(const String& find, const String& replace)
646646
readFrom = foundAt + replace.len;
647647
}
648648
} else if (diff < 0) {
649-
char *writeTo = buffer;
649+
unsigned int size = len; // compute size needed for result
650650
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
651-
unsigned int n = foundAt - readFrom;
652-
memcpy(writeTo, readFrom, n);
653-
writeTo += n;
654-
memcpy(writeTo, replace.buffer, replace.len);
655-
writeTo += replace.len;
656651
readFrom = foundAt + find.len;
657-
len += diff;
652+
diff = 0 - diff;
653+
size -= diff;
654+
}
655+
if (size == len) return;
656+
int index = len - 1;
657+
while (index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
658+
readFrom = buffer + index + find.len;
659+
memmove(readFrom - diff, readFrom, len - (readFrom - buffer));
660+
len -= diff;
661+
buffer[len] = 0;
662+
memcpy(buffer + index, replace.buffer, replace.len);
663+
index--;
658664
}
659-
strcpy(writeTo, readFrom);
660665
} else {
661666
unsigned int size = len; // compute size needed for result
662667
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {

Diff for: api/String.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class String
107107
unsigned char concat(const String &str);
108108
unsigned char concat(const char *cstr);
109109
unsigned char concat(char c);
110-
unsigned char concat(unsigned char c);
110+
unsigned char concat(unsigned char num);
111111
unsigned char concat(int num);
112112
unsigned char concat(unsigned int num);
113113
unsigned char concat(long num);

Diff for: test/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build

0 commit comments

Comments
 (0)