Skip to content

Commit

Permalink
writing mock pico code and working on setting up testbase file
Browse files Browse the repository at this point in the history
  • Loading branch information
Darrow8 committed Oct 29, 2024
1 parent a590399 commit a4ffbc8
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 3 deletions.
1 change: 1 addition & 0 deletions mock_pico/printf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <stdio.h>
42 changes: 42 additions & 0 deletions mock_pico/stdlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef MOCK_PICO_STDLIB_H

Check notice on line 1 in mock_pico/stdlib.h

View workflow job for this annotation

GitHub Actions / Lint-Check

Run clang-format on mock_pico/stdlib.h

File mock_pico/stdlib.h does not conform to Custom style guidelines. (lines 4, 10, 15, 16, 20, 25, 31, 36)
#define MOCK_PICO_STDLIB_H

#include "mock_pico/printf.h"
#include <stdint.h>
#include <stdbool.h>
#include "mock_pico/types.h"

// Mock for GPIO pin initialization
void gpio_init(uint pin) {
printf("Mock: GPIO pin %d initialized\n", pin);
}

// Mock for setting GPIO direction
void gpio_set_dir(uint pin, bool out) {
printf("Mock: GPIO pin %d set to direction %s\n", pin, out ? "OUTPUT" : "INPUT");
}

// Mock for setting GPIO state
void gpio_put(uint pin, bool value) {
printf("Mock: GPIO pin %d set to %s\n", pin, value ? "HIGH" : "LOW");
}

// Mock for reading GPIO state
bool gpio_get(uint pin) {
printf("Mock: GPIO pin %d read\n", pin);
return true; // Simulate high signal
}

// Mock for sleep/delay function
void sleep_ms(uint32_t ms) {
printf("Mock: Sleep for %d ms\n", ms);
}

// Mock for UART initialization (if used)
void uart_init(void) {
printf("Mock: UART initialized\n");
}

// You can add more mocked functions based on what you use in your code

#endif // MOCK_PICO_STDLIB_H
25 changes: 25 additions & 0 deletions mock_pico/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef MOCK_PICO_TYPES_H

Check notice on line 1 in mock_pico/types.h

View workflow job for this annotation

GitHub Actions / Lint-Check

Run clang-format on mock_pico/types.h

File mock_pico/types.h does not conform to Custom style guidelines. (lines 14, 19)
#define MOCK_PICO_TYPES_H

#include <sys/types.h>

// Add any basic types or stubs needed for your code here
// typedef unsigned int uint;
// typedef unsigned char uint8_t;
// typedef unsigned long long uint64_t;
// typedef unsigned int uint32_t;
// typedef unsigned long size_t;
// Mock absolute_time_t type to match pico-sdk
#if PICO_OPAQUE_ABSOLUTE_TIME_T
typedef struct {
uint64_t _private_us_since_boot;
} absolute_time_t;
#else
typedef uint64_t absolute_time_t;
#endif



// Define other Pico types as needed for your code

#endif // MOCK_PICO_TYPES_H
5 changes: 4 additions & 1 deletion src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@

#include "error.h"
#include "macros.h"
#ifdef TEST
#include "mock_pico/stdlib.h"
#else
#include "pico/stdlib.h"

#endif
/**
* This function should be called if we encounter an unrecoverable error. In
* non-flight builds, enter a panic state.
Expand Down
4 changes: 4 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

#include "init.h"
#include "macros.h"
#ifdef TEST
#include "mock_pico/stdlib.h"
#else
#include "pico/stdlib.h"
#endif
#include "scheduler/scheduler.h"

/**
Expand Down
27 changes: 27 additions & 0 deletions src/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
#pragma once

#include "error.h"

#ifdef TEST
#include "mock_pico/printf.h"
#else
#include "pico/printf.h"
#endif

/**
* If this symbol is defined, we are configured to run a flight build.
Expand Down Expand Up @@ -41,6 +46,28 @@
#define LOG_DEBUG(fmt, ...) (void)0
#endif


/**
* Convenience macros to get whether we are in test mode in a runtime build.
*/
#ifdef TEST
#define IS_TEST true
#else
#define IS_TEST false
#endif

/**
* Log a formatted message at the debug level. Will only do anything in a
* test build.
*/
#ifndef TEST
#define LOG_TEST(fmt, ...) \
printf("[TEST] " fmt "\n" __VA_OPT__(, ) __VA_ARGS__)
#else
#define LOG_TEST(fmt, ...) (void)0
#endif


/**
* Log a printf-style formatted message at the info level. Will log in both
* flight and test builds.
Expand Down
17 changes: 15 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@

#include "init.h"
#include "macros.h"

#ifdef TEST
#include "mock_pico/stdlib.h" // Use mock version for local testing
#else
#include "pico/stdlib.h"
#endif

#include "scheduler/scheduler.h"
#include "slate.h"
#include "testbase.h"

/**
* Statically allocate the slate.
Expand All @@ -23,10 +30,16 @@ slate_t slate;
*/
int main()
{
// Some ugly code with linter errors
int x = 10 + 5;
stdio_init_all();

/*
* If we are in test mode, run the tests and exit.
*/
if (IS_TEST)
{
return test_main();
}

/*
* In debug builds, delay to allow the user to connect to open the serial
* port.
Expand Down
4 changes: 4 additions & 0 deletions src/scheduler/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

#pragma once

#ifdef TEST
#include "../mock_pico/types.h"
#else
#include "pico/types.h"
#endif

#define MAX_TASKS_PER_STATE 10

Expand Down
5 changes: 5 additions & 0 deletions src/slate.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@

#pragma once

#ifdef TEST
#include "../mock_pico/types.h"
#else
#include "pico/types.h"
#endif

#include "scheduler/scheduler.h"

typedef struct samwise_slate
Expand Down
Empty file.
32 changes: 32 additions & 0 deletions src/testbase.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**

Check notice on line 1 in src/testbase.c

View workflow job for this annotation

GitHub Actions / Lint-Check

Run clang-format on src/testbase.c

File src/testbase.c does not conform to Custom style guidelines. (lines 9, 15, 29)
* @author Darrow Hartman
* @date 2024-10-28
*
* This file contains the main testing entry point for the SAMWISE flight code.
* All testing code should import this file
*/

#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>

#include "init.h"
#include "macros.h"
#include "scheduler/scheduler.h"
#include "slate.h"


/**
* Static allocation of the slate.
*/
slate_t test_slate;

int test_main()
{
stdio_init_all();
LOG_TEST("testbase: Slate uses %d bytes", sizeof(test_slate));
LOG_TEST("testbase: Initializing everything...");
ASSERT(init(&test_slate));
LOG_TEST("testbase: We are in test mode!");

return 0;
}
14 changes: 14 additions & 0 deletions src/testbase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**

Check notice on line 1 in src/testbase.h

View workflow job for this annotation

GitHub Actions / Lint-Check

Run clang-format on src/testbase.h

File src/testbase.h does not conform to Custom style guidelines. (lines 6)
* @author Darrow Hartman
* @date 2024-10-28
*/

#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>

#include "init.h"
#include "macros.h"
#include "scheduler/scheduler.h"
#include "slate.h"

int test_main(void);

0 comments on commit a4ffbc8

Please sign in to comment.