Skip to content

Commit

Permalink
feat: add new tests, and fix the layer functions
Browse files Browse the repository at this point in the history
  • Loading branch information
headblockhead committed Aug 21, 2024
1 parent f7c76d4 commit 5c100cf
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 72 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ elseif(CMAKE_BUILD_TYPE STREQUAL "Testing")
add_executable(layer_press_release tests/layer_press_release.c)
target_link_libraries(layer_press_release squirrel)
add_test(NAME layer_press_release COMMAND layer_press_release)

add_executable(consumer_activate_deactivate_get_consumer_code tests/consumer_activate_deactivate_get_consumer_code.c)
target_link_libraries(consumer_activate_deactivate_get_consumer_code squirrel)
add_test(NAME consumer_activate_deactivate_get_consumer_code COMMAND consumer_activate_deactivate_get_consumer_code)

add_executable(keyboard_activate_deactivate_keycode tests/keyboard_activate_deactivate_keycode.c)
target_link_libraries(keyboard_activate_deactivate_keycode squirrel)
add_test(NAME keyboard_activate_deactivate_keycode COMMAND keyboard_activate_deactivate_keycode)

add_executable(keyboard_activate_deactivate_get_modifier tests/keyboard_activate_deactivate_get_modifier.c)
target_link_libraries(keyboard_activate_deactivate_get_modifier squirrel)
add_test(NAME keyboard_activate_deactivate_get_modifier COMMAND keyboard_activate_deactivate_get_modifier)
else()
add_compile_options(-Os) # Enable size optimizations
endif()
Expand Down
38 changes: 23 additions & 15 deletions include/squirrel_quantum.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,48 +52,56 @@ enum squirrel_error consumer_release(struct key *key, uint8_t layer,
void **args);

// quantum_passthrough_press passes the press action to the highest active layer
// below the current one. It expectes no extra args.
// below the current one. It expectes no extra args. Equivalent to KC_TRNS in
// QMK.
enum squirrel_error quantum_passthrough_press(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);

// quantum_passthrough_release passes the release action to the highest active
// layer below the current one. It expectes no extra args.
// layer below the current one. It expectes no extra args. Equivalent to KC_TRNS
// in QMK.
enum squirrel_error quantum_passthrough_release(struct key *key, uint8_t layer,
uint8_t key_index,
int arg_count, void **args);

// layer_momentary_press activates the layer with the given index. It expects
// the layer number as the first uint8 argument.
// the layer number as the first uint8 argument. Equivalent to MO() in QMK.
enum squirrel_error layer_momentary_press(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);

// layer_momentary_release deactivates the layer with the given index. It
// expects the layer number as the first uint8 argument.
// expects the layer number as the first uint8 argument. Equivalent to MO() in
// QMK.
enum squirrel_error layer_momentary_release(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);

// layer_toggle_press toggles the layer with the given index. It expects the
// layer number as the first uint8 argument.
// layer number as the first uint8 argument. Equivalent to TG() in QMK.
enum squirrel_error layer_toggle_press(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);

// layer_toggle_release does nothing at the moment.
// layer_toggle_release does nothing at the moment. It expects the layer number
// as a uint8 anyway - this is a placeholder for future functionality.
// Equivalent to TG() in QMK.
enum squirrel_error layer_toggle_release(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);

// layer_turn_on_press activates the layer with the given index. It expects the
// layer number as the first uint8 argument.
enum squirrel_error layer_turn_on_press(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);
// layer_solo_press turns off all other layers than the layer with the given
// index. It expects the layer number as the first uint8 argument. Equivalent to
// TO() in QMK.
enum squirrel_error layer_solo_press(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);

// layer_turn_on_release does nothing at the moment.
enum squirrel_error layer_turn_on_release(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);
// layer_solo_release does nothing at the moment. It expects the layer number
// as a uint8 anyway - this is a placeholder for future functionality.
// Equivalent to TO() in QMK.
enum squirrel_error layer_solo_release(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args);
#endif
41 changes: 35 additions & 6 deletions src/squirrel_quantum.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,35 +154,64 @@ enum squirrel_error quantum_passthrough_release(struct key *key, uint8_t layer,
enum squirrel_error layer_momentary_press(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args) {
if (arg_count != 1) {
return ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT;
};
uint8_t target_layer = *(uint8_t *)args[0];
layers[target_layer].active = true;
return ERR_NONE;
}

enum squirrel_error layer_momentary_release(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args) {
if (arg_count != 1) {
return ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT;
};
uint8_t target_layer = *(uint8_t *)args[0];
layers[target_layer].active = false;
return ERR_NONE;
}

enum squirrel_error layer_toggle_press(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args) {
if (arg_count != 1) {
return ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT;
};
uint8_t target_layer = *(uint8_t *)args[0];
layers[target_layer].active = !layers[target_layer].active;
return ERR_NONE;
}

enum squirrel_error layer_toggle_release(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args) {
if (arg_count != 1) {
return ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT;
};
return ERR_NONE;
}

enum squirrel_error layer_turn_on_press(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args) {
enum squirrel_error layer_solo_press(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args) {
if (arg_count != 1) {
return ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT;
};
uint8_t target_layer = *(uint8_t *)args[0];
for (uint8_t i = 0; i < 16; i++) {
layers[i].active = false;
}
layers[target_layer].active = true;
return ERR_NONE;
}

enum squirrel_error layer_turn_on_release(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args) {
enum squirrel_error layer_solo_release(struct key *key, uint8_t layer,
uint8_t key_index, int arg_count,
void **args) {
if (arg_count != 1) {
return ERR_KEY_FUNC_WRONG_ARGUMENT_COUNT;
};
return ERR_NONE;
}
24 changes: 24 additions & 0 deletions tests/consumer_activate_deactivate_get_consumer_code.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "squirrel.h"
#include "squirrel_consumer.h"
#include "squirrel_key.h"
#include "squirrel_quantum.h"
#include <stdint.h>
#include <stdlib.h>

// test: consumer_activate_consumer_code + consumer_deactivate_consumer_code +
// consumer_get_consumer_code - in squirrel_consumer.c
int main() {
for (uint16_t test_consumer_code = 0; test_consumer_code <= 65534;
test_consumer_code++) {
// consumer_activate_consumer_code
consumer_activate_consumer_code(test_consumer_code);
if (consumer_get_consumer_code() != test_consumer_code) {
return 1;
}
// consumer_deactivate_consumer_code
consumer_deactivate_consumer_code(test_consumer_code);
if (consumer_get_consumer_code() != 0) {
return 2;
}
}
};
49 changes: 46 additions & 3 deletions tests/key.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ int main() {
init_keyboard(1);

// press_key + release_key

uint8_t code1 = 0x0F;
uint8_t code2 = 0xF0;

Expand All @@ -65,6 +64,7 @@ int main() {
layers[0].keys[0] = testkey;
layers[0].active = true;

// check that arguments are correct, and in the correct order.
enum squirrel_error err = press_key(0);
if (err != ERR_NONE) {
return 2;
Expand All @@ -78,11 +78,54 @@ int main() {
if (err != ERR_NONE) {
return 4;
}
if (test_result != 0) {
return 5;
}

free(testkey.pressed_arguments);
free(testkey.released_arguments);

// TODO: check_key
// check_key

test_result = 1;
testkey.is_pressed = false;
check_key(0, true); // should call press_key
if (test_result != 0) {
return 6;
}
if (testkey.is_pressed != true) {
return 7;
}

test_result = 1;
testkey.is_pressed = true;
check_key(0, false); // should call release_key
if (test_result != 0) {
return 8;
}
if (testkey.is_pressed != false) {
return 9;
}

test_result = 1;
testkey.is_pressed = true;
check_key(0, true); // should not call press_key
if (test_result != 1) {
return 10;
}
if (testkey.is_pressed != true) {
return 11;
}

test_result = 1;
testkey.is_pressed = false;
check_key(0, false); // should not call release_key
if (test_result != 1) {
return 12;
}
if (testkey.is_pressed != false) {
return 13;
}

return test_result;
return 0;
}
37 changes: 37 additions & 0 deletions tests/keyboard_activate_deactivate_get_modifier.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "squirrel.h"
#include "squirrel_consumer.h"
#include "squirrel_key.h"
#include "squirrel_quantum.h"
#include <stdint.h>
#include <stdlib.h>

// test: keyboard_activate_modifier + keyboard_deactivate_modifier +
// keyboard_get_modifiers - in squirrel_keyboard.c
int main() {
for (uint8_t test_modifier = 0b00000001; test_modifier != 0b00000000;
test_modifier = test_modifier << 1) {
for (uint8_t test_modifier_2 = 0b10000000; test_modifier_2 != 0b00000000;
test_modifier_2 = test_modifier_2 >> 1) {
if (test_modifier == test_modifier_2) { // Skip if the same modifier
continue;
}
keyboard_activate_modifier(test_modifier);
if (keyboard_get_modifiers() != test_modifier) {
return 1;
}
keyboard_activate_modifier(test_modifier_2);
if (keyboard_get_modifiers() != (test_modifier | test_modifier_2)) {
return 2;
}
// keyboard_deactivate_modifier
keyboard_deactivate_modifier(test_modifier);
if (keyboard_get_modifiers() != test_modifier_2) {
return 3;
}
keyboard_deactivate_modifier(test_modifier_2);
if (keyboard_get_modifiers() != 0) {
return 4;
}
}
}
};
23 changes: 23 additions & 0 deletions tests/keyboard_activate_deactivate_keycode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "squirrel.h"
#include "squirrel_consumer.h"
#include "squirrel_key.h"
#include "squirrel_quantum.h"
#include <stdint.h>
#include <stdlib.h>

// test: keyboard_activate_keycode + keyboard_deactivate_keycode in
// squirrel_keyboard.c
int main() {
for (uint8_t test_keycode = 0; test_keycode <= 254; test_keycode++) {
// keyboard_activate_keycode
keyboard_activate_keycode(test_keycode);
if (keyboard_keycodes[test_keycode] != true) {
return 1;
}
// keyboard_deactivate_keycode
keyboard_deactivate_keycode(test_keycode);
if (keyboard_keycodes[test_keycode] != false) {
return 2;
}
}
};
Loading

0 comments on commit 5c100cf

Please sign in to comment.