Skip to content

Commit 0781ef8

Browse files
James Fosterjgfoster
James Foster
andauthored
Mock for testing (#1)
* Allow code to compile on x86_64 platform. * Add Arduino-CI support. * File tests pass! * Use jgfoster's arduino_ci. * Update test script. * Try updates to tests. * Refactor so that File_CI points to shared ata. * Tests pass? * Use byte-oriented buffer rather than String for contents. * Fix memory leak. * Basic directory functionality with tests. * WIP * WIP on file open. * WIP * WIP * Mock SD and File with tests. Co-authored-by: James Foster <[email protected]>
1 parent caece65 commit 0781ef8

File tree

13 files changed

+770
-0
lines changed

13 files changed

+770
-0
lines changed

.arduino-ci.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
unittest:
3+
platforms:
4+
- mega2560
5+
6+
compile:
7+
platforms:
8+
- mega2560

.github/workflows/arduino_ci.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# This is the name of the workflow, visible on GitHub UI
2+
name: Arduino CI
3+
4+
# Run on a Push or a Pull Request
5+
on: [push, pull_request]
6+
7+
jobs:
8+
arduino_ci:
9+
runs-on: ubuntu-latest
10+
steps:
11+
# Clone the repo using the `checkout` action
12+
- uses: actions/checkout@v2
13+
14+
- uses: ruby/setup-ruby@v1
15+
with:
16+
ruby-version: 2.6
17+
- name: Check style, functionality, and usage
18+
run: |
19+
g++ -v
20+
scripts/test.sh

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
Gemfile.lock
2+
.bundle
3+
vendor
4+
*.cpp.bin*
5+
.vscode
6+
libarduino.so*
7+
18
#################
29
## Eclipse
310
#################
@@ -213,3 +220,4 @@ pip-log.txt
213220

214221
#Mr Developer
215222
.mr.developer.cfg
223+
doc/html

Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
source 'https://rubygems.org'
2+
gem 'arduino_ci', git: 'https://github.com/jgfoster/arduino_ci', branch: 'main'

scripts/test.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#! /bin/sh
2+
bundle config --local path vendor/bundle
3+
bundle install
4+
if [ -z "$1" ]; then
5+
bundle exec arduino_ci.rb --skip-examples-compilation
6+
else
7+
bundle exec arduino_ci.rb --skip-examples-compilation --testfile-select="$1"
8+
fi

src/ArduinoCI/File_CI.cpp

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#include "SD_CI.h"
2+
3+
#ifdef MOCK_PINS_COUNT
4+
5+
#include <cassert>
6+
7+
file_ci::file_ci(String name) {
8+
this->name = name;
9+
this->contents = static_cast<uint8_t *>(malloc(0));
10+
this->size = 0;
11+
}
12+
13+
file_ci::~file_ci() { free(this->contents); }
14+
15+
/*
16+
#define O_RDONLY 0x00 ///< Open for reading only.
17+
#define O_WRONLY 0x01 ///< Open for writing only.
18+
#define O_RDWR 0x02 ///< Open for reading and writing.
19+
#define O_AT_END 0x04 ///< Open at EOF.
20+
#define O_APPEND 0x08 ///< Set append mode.
21+
#define O_TRUNC 0x20 ///< Truncate this->file to zero length.
22+
*/
23+
File_CI::File_CI(file_ci *file, oflag_t oflag) {
24+
this->file = file;
25+
this->oflag = oflag;
26+
if (oflag & O_TRUNC) {
27+
if (this->file->size) {
28+
free(this->file->contents);
29+
this->file->contents = static_cast<uint8_t *>(malloc(0));
30+
this->file->size = 0;
31+
}
32+
this->_position = 0;
33+
} else if (oflag & O_AT_END) {
34+
this->_position = this->size();
35+
}
36+
}
37+
38+
uint32_t File_CI::available32() const {
39+
return this->size() - this->position();
40+
}
41+
42+
bool File_CI::close() {
43+
bool flag = this->file != nullptr;
44+
this->file = nullptr;
45+
return flag;
46+
}
47+
48+
void File_CI::flush() { assert(this->file != nullptr); }
49+
50+
size_t File_CI::getName(char *name, size_t size) {
51+
assert(this->file != nullptr);
52+
strncpy(name, this->file->name.c_str(), size);
53+
return min(size, this->file->name.length());
54+
}
55+
56+
const char *File_CI::name() const {
57+
assert(this->file != nullptr);
58+
return this->file->name.c_str();
59+
}
60+
61+
uint32_t File_CI::position() const {
62+
assert(this->file != nullptr);
63+
return this->_position;
64+
}
65+
66+
int File_CI::read() {
67+
assert(this->file != nullptr);
68+
if (this->oflag == O_WRONLY) {
69+
return -1;
70+
}
71+
if (this->available32()) {
72+
uint8_t *ptr =
73+
static_cast<uint8_t *>(this->file->contents + this->_position);
74+
int value = *ptr;
75+
++(this->_position);
76+
return value;
77+
} else {
78+
return -1;
79+
}
80+
}
81+
82+
int File_CI::read(void *buf, size_t count) {
83+
assert(this->file != nullptr);
84+
if (this->oflag == O_WRONLY) {
85+
return -1;
86+
}
87+
int size = min(count, this->available32());
88+
memcpy(buf, this->file->contents + this->_position, size);
89+
this->_position += size;
90+
return size;
91+
}
92+
93+
void File_CI::rewind() {
94+
assert(this->file != nullptr);
95+
this->_position = 0;
96+
}
97+
98+
bool File_CI::seek(uint32_t pos) {
99+
assert(this->file != nullptr);
100+
this->_position = pos;
101+
return true;
102+
}
103+
104+
bool File_CI::seekEnd(int32_t offset) {
105+
assert(this->file != nullptr);
106+
this->seek(this->size() + offset);
107+
return true;
108+
}
109+
110+
uint32_t File_CI::size() const {
111+
assert(this->file != nullptr);
112+
return this->file->size;
113+
}
114+
115+
bool File_CI::truncate() {
116+
assert(this->file != nullptr);
117+
uint8_t *ptr = static_cast<uint8_t *>(malloc(this->_position));
118+
if (!ptr) {
119+
return false;
120+
}
121+
memcpy(ptr, this->file->contents, this->_position);
122+
free(this->file->contents);
123+
this->file->contents = ptr;
124+
this->file->size = this->_position;
125+
return true;
126+
}
127+
128+
bool File_CI::truncate(uint32_t length) {
129+
assert(this->file != nullptr);
130+
this->_position = length;
131+
return this->truncate();
132+
}
133+
134+
size_t File_CI::write(const char *str) {
135+
assert(this->file != nullptr);
136+
if (this->oflag == O_RDONLY) {
137+
return -1;
138+
}
139+
return this->write(static_cast<const void *>(str), strlen(str));
140+
}
141+
142+
size_t File_CI::write(const void *buf, size_t count) {
143+
assert(this->file != nullptr);
144+
if (this->oflag == O_RDONLY) {
145+
return -1;
146+
}
147+
if (this->available32() < count) {
148+
uint8_t *ptr = static_cast<uint8_t *>(malloc(this->_position + count));
149+
if (!ptr) {
150+
return -1;
151+
}
152+
memcpy(ptr, this->file->contents, this->_position);
153+
free(this->file->contents);
154+
this->file->contents = ptr;
155+
this->file->size = this->_position + count;
156+
}
157+
memcpy(this->file->contents + this->_position, buf, count);
158+
this->_position += count;
159+
return count;
160+
}
161+
162+
File_CI::operator bool() { return this->file != nullptr; }
163+
164+
#endif

0 commit comments

Comments
 (0)