1
0
Fork 0
mirror of https://github.com/mfocko/blog.git synced 2025-05-12 06:12:57 +02:00

chore: transfer all KBs to single one

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-11-05 15:24:54 +01:00
parent d207e870d4
commit 7427475022
Signed by: mfocko
GPG key ID: 7C47D46246790496
159 changed files with 28847 additions and 0 deletions
static/files/pb071/bonuses/04

View file

@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.0)
# Project configuration
project(seminar04-bonus-maze)
set(SOURCES maze.h maze.c)
set(EXECUTABLE maze)
# Executable
add_executable(maze ${SOURCES} main.c)
add_executable(test_maze ${SOURCES} cut.h test_maze.c)
# Configure compiler warnings
if (CMAKE_C_COMPILER_ID MATCHES Clang OR ${CMAKE_C_COMPILER_ID} STREQUAL GNU)
# using regular Clang, AppleClang or GCC
# Strongly suggested: neable -Werror
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11 -Wall -Wextra -pedantic")
elseif (${CMAKE_C_COMPILER_ID} STREQUAL MSVC)
# using Visual Studio C++
target_compile_definitions(${EXECUTABLE} PRIVATE _CRT_SECURE_NO_DEPRECATE)
set(CMAKE_CXX_FLAGS "/permissive- /W4 /EHsc")
endif()
if(MINGW)
target_compile_definitions(${EXECUTABLE} PRIVATE __USE_MINGW_ANSI_STDIO=1)
endif()

View file

@ -0,0 +1,8 @@
#include "maze.h"
#include <stdio.h>
int main(void)
{
return 0;
}

View file

@ -0,0 +1,84 @@
#include "maze.h"
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
/**
* @brief Checks if pointer points inside the memory specified by upper and lower
* bound.
* @param first Start of the allocated memory.
* @param last First address that is not included in the allocated memory.
* @param pos Pointer to memory.
* @returns <code>true</code> if pos points to the memory [first, last), <code>
* false</code> otherwise.
*/
static bool within_bounds(const char *first, const char *last, const char *pos)
{
/* TODO */
}
/**
* @brief Sets 2D coordinates of the pointer.
* @param map Start of the 2D memory.
* @param position Pointer to the map.
* @param width Width of one row of the map.
* @param row Output variable where the row is set.
* @param col Output variable where the column is set.
*/
static void set_coordinates(const char *map, char *position, size_t width, size_t *row, size_t *col)
{
/* TODO */
}
/**
* @brief Checks if indices are within bounds of the 2D map.
* @param width Width of the map.
* @param height Height of the map.
* @param row Row to be checked.
* @param col Column to be checked.
* @returns <code>true</code> if row,col are within bounds of the map,.<code>false
* </code> otherwise.
*/
static bool within_bounds_by_index(size_t width, size_t height, size_t row, size_t col)
{
/* TODO */
}
void print_maze(const char *map, char *position, char direction, size_t width, size_t height)
{
printf("Maze:\n");
for (size_t row = 0; row < height; row++) {
for (size_t col = 0; col < width; col++) {
const char *current = &map[row * width + col];
if (current == position) {
switch (direction) {
case '^':
putchar('N');
break;
case 'v':
putchar('S');
break;
case '>':
putchar('E');
break;
case '<':
putchar('W');
break;
}
continue;
}
putchar(*current);
}
putchar('\n');
}
putchar('\n');
}
enum end_state_t walk(const char *map, char *position, char direction, size_t width, size_t height)
{
/* TODO */
return NONE;
}

View file

@ -0,0 +1,33 @@
#include <stdlib.h>
enum end_state_t
{
NONE,
FOUND_KEY,
FOUND_TREASURE,
OUT_OF_BOUNDS,
INFINITE_LOOP,
};
/**
* @brief Prints maze and the robot within it.
* @param map Map of the maze.
* @param position Current position of the robot.
* @param direction Direction the robot is facing, one of "^v<>".
* @param width Width of the map.
* @param height Height of the map.
*/
void print_maze(const char *map, char *position, char direction, size_t width, size_t height);
/**
* @brief Get end state of the robot after his walk.
* @param map Map of the maze.
* @param position Initial position of the robot in the maze.
* @param direction Direction the robot is facing at the beginning. You can assume
* correctness of this value.
* @param width Width of the maze. You can assume correctness of this value.
* @param height Height of the maze. You can assume correctness of this value.
* @returns End state of the robot after his walk is finished or has been terminated
* manually.
*/
enum end_state_t walk(const char *map, char *position, char direction, size_t width, size_t height);

View file

@ -0,0 +1,246 @@
#include "maze.h"
#define CUT_MAIN
#include "cut.h"
static void check_result(char *map, size_t position, char direction, size_t width, size_t height, enum end_state_t expected)
{
enum end_state_t state = walk(map, &map[position], direction, width, height);
ASSERT(state == expected);
}
TEST(basic)
{
SUBTEST(spawns_on_treasure)
{
check_result(("T."
".."),
0,
'>',
2,
2,
FOUND_TREASURE);
}
SUBTEST(spawns_on_key)
{
check_result(("K."
".."),
0,
'^',
2,
2,
FOUND_KEY);
}
SUBTEST(spawns_on_treasure_in_middle)
{
check_result(("..."
".T."
"..."),
4,
'v',
3,
3,
FOUND_TREASURE);
}
}
TEST(some_walking)
{
SUBTEST(straight_up)
{
check_result((".T."
"..."
"..."),
7,
'^',
3,
3,
FOUND_TREASURE);
}
SUBTEST(down)
{
check_result(("..........................T"), 7, 'v', 1, 27, FOUND_TREASURE);
}
SUBTEST(to_right)
{
check_result(("..........................K"), 2, '>', 27, 1, FOUND_KEY);
}
SUBTEST(to_left)
{
check_result(("K.........................."), 15, '<', 27, 1, FOUND_KEY);
}
}
TEST(follows_directions)
{
SUBTEST(basic)
{
check_result((">..v"
"...."
"...K"
"^..<"),
12,
'>',
4,
4,
FOUND_KEY);
}
SUBTEST(no_way_to_avoid)
{
const char *directions = "<>^v";
for (size_t row = 0; row < 5; row++) {
for (size_t col = 0; col < 5; col++) {
for (size_t dir = 0; dir < 4; dir++) {
check_result((">>>>v"
"^>>vv"
"^^T<v"
"^^<<<"
"^<<<<"),
row * 5 + col,
directions[dir],
5,
5,
FOUND_TREASURE);
}
}
}
}
}
TEST(follows_directions_even_to_doom)
{
SUBTEST(to_right)
{
check_result((".>.."
"^KTK"
".TvT"
"...<"),
1,
'>',
4,
4,
OUT_OF_BOUNDS);
}
SUBTEST(to_up)
{
check_result((".>.."
"^KTK"
".TvT"
"...<"),
4,
'>',
4,
4,
OUT_OF_BOUNDS);
}
SUBTEST(to_down)
{
check_result((".>.."
"^KTK"
".TvT"
"...<"),
10,
'>',
4,
4,
OUT_OF_BOUNDS);
}
SUBTEST(to_left)
{
check_result((".>.."
"^KTK"
".TvT"
"...<"),
15,
'>',
4,
4,
OUT_OF_BOUNDS);
}
}
TEST(mean)
{
SUBTEST(out_of_bounds_at_the_beginning)
{
check_result((".."
".."),
4,
'v',
2,
2,
OUT_OF_BOUNDS);
check_result((".."
".."),
5,
'v',
2,
2,
OUT_OF_BOUNDS);
check_result((".."
".."),
(size_t) -1,
'v',
2,
2,
OUT_OF_BOUNDS);
check_result((">T<"
"..."
"^.^"),
10,
'^',
3,
3,
OUT_OF_BOUNDS);
}
}
TEST(infinite_bonus)
{
SUBTEST(easy)
{
check_result((">.v"
"..."
"^.<"),
0,
'v',
3,
3,
INFINITE_LOOP);
}
SUBTEST(medium)
{
check_result((">v"
"^<"),
2,
'v',
2,
2,
INFINITE_LOOP);
}
SUBTEST(hard)
{
check_result(("v.v"
"..."
"^.^"),
0,
'>',
3,
3,
INFINITE_LOOP);
}
SUBTEST(harder)
{
check_result((">.v.."
"....."
"^...<"
"....."
"..>.^"),
4,
'v',
5,
5,
INFINITE_LOOP);
}
}