Skip to content
Extraits de code Groupes Projets
Valider daf3a7f6 rédigé par Louis Navarre's avatar Louis Navarre
Parcourir les fichiers

Add skeleton

parent 838f4244
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
CC=gcc
CFLAGS=-Wall -Werror -g
LIBS=-lcunit -lpthread
INCLUDE_HEADERS_DIRECTORY=-Iheaders
fec: main.c # add your other object files needed to compile your program here. !! The ordering is important !! if file_a.o depends on file_b.o, file_a.o must be placed BEFORE file_b.o in the list !
$(CC) $(INCLUDE_HEADERS_DIRECTORY) $(CFLAGS) -o $@ $^ $(LIBS) # this will run the following command: gcc -Wall -Werror -g -o kmeans src/distance.o other_object_filespresent_above.o ... -lcunit -lpthread
%.o: %.c # if for example you want to compute example.c this will create an object file called example.o in the same directory as example.c. Don't forget to clean it in your "make clean"
$(CC) $(INCLUDE_HEADERS_DIRECTORY) $(CFLAGS) -o $@ -c $<
clean:
rm -f src/*.o
rm -f fec
rm -f test_tinymt32
tests: tests/test_tinymt32.c
$(CC) -o test_tinymt32 tests/test_tinymt32.c -lcunit
./test_tinymt32
echo "TODO !"
# a .PHONY target forces make to execute the command even if the target already exists
.PHONY: clean tests
#ifndef BLOCK_H
#define BLOCK_H
typedef struct
{
// TODO
} block_t;
#endif /* BLOCK_H */
\ No newline at end of file
Ce diff est replié.
#ifndef MESSAGE_H
#define MESSAGE_H
typedef struct
{
// TODO
} message_t;
#endif /* MESSAGE_H */
\ No newline at end of file
#ifndef PORTABLE_ENDIAN_H
#define PORTABLE_ENDIAN_H
// source - https://stackoverflow.com/questions/20813028/endian-h-not-found-on-mac-osx
#ifdef __APPLE__
#include <machine/endian.h>
#include <libkern/OSByteOrder.h>
#define htobe16(x) OSSwapHostToBigInt16(x)
#define htole16(x) OSSwapHostToLittleInt16(x)
#define be16toh(x) OSSwapBigToHostInt16(x)
#define le16toh(x) OSSwapLittleToHostInt16(x)
#define htobe32(x) OSSwapHostToBigInt32(x)
#define htole32(x) OSSwapHostToLittleInt32(x)
#define be32toh(x) OSSwapBigToHostInt32(x)
#define le32toh(x) OSSwapLittleToHostInt32(x)
#define htobe64(x) OSSwapHostToBigInt64(x)
#define htole64(x) OSSwapHostToLittleInt64(x)
#define be64toh(x) OSSwapBigToHostInt64(x)
#define le64toh(x) OSSwapLittleToHostInt64(x)
#define __BIG_ENDIAN BIG_ENDIAN
#define __LITTLE_ENDIAN LITTLE_ENDIAN
#define __BYTE_ORDER BYTE_ORDER
#else
#include <endian.h>
#endif /* __APPLE__ */
#endif /* PORTABLE_ENDIAN_H */
\ No newline at end of file
#ifndef PORTABLE_SEMAPHORE_H
#define PORTABLE_SEMAPHORE_H
#include <semaphore.h>
#include <stdint.h>
static inline sem_t *
my_sem_init_with_name(char *name, uint32_t value)
{
#ifdef __APPLE__
int rc = sem_unlink(name);
sem_t *s;
return sem_open(name, O_CREAT, 0644, value);
#else
sem_t *sem = malloc(sizeof(sem_t));
if (!sem)
{
return NULL;
}
if (sem_init(sem, 0, value) != 0)
{
free(sem);
return NULL;
}
return sem;
#endif /* __APPLE__ */
}
static inline sem_t *
my_sem_init(uint32_t value)
{
return my_sem_init_with_name("/tmp/myPortableSem", value);
}
static inline int
my_sem_destroy(sem_t *sem)
{
#ifdef __APPLE__
return sem_close(sem);
#else
int err = sem_destroy(sem);
free(sem);
#endif /* __APPLE__ */
}
// Define to hide the portable semaphore
#define sem_init(value) my_sem_init(value)
#define sem_destroy(sem) my_sem_destroy(sem)
// sem_wait and sem_post are equivalent for OSX and Linux => use default function calls
#endif /* PORTABLE_SEMAPHORE_H */
\ No newline at end of file
#ifndef SYSTEM_H
#define SYSTEM_H
typedef struct
{
// TODO
} system_t;
#endif /* SYSTEM_H */
\ No newline at end of file
#ifndef TINYMT32_H
#define TINYMT32_H
/**
* @file tinymt32.h
*
* @brief Tiny Mersenne Twister only 127 bit internal state
*
* @author Mutsuo Saito (Hiroshima University)
* @author Makoto Matsumoto (University of Tokyo)
*
* Copyright (C) 2011 Mutsuo Saito, Makoto Matsumoto,
* Hiroshima University and The University of Tokyo.
* All rights reserved.
*
* The 3-clause BSD License is applied to this software, see
* LICENSE.txt
*/
#include <stdint.h>
#include <inttypes.h>
#define TINYMT32_MEXP 127
#define TINYMT32_SH0 1
#define TINYMT32_SH1 10
#define TINYMT32_SH8 8
#define TINYMT32_MASK UINT32_C(0x7fffffff)
#define TINYMT32_MUL (1.0f / 16777216.0f)
/**
* tinymt32 internal state vector and parameters
*/
struct TINYMT32_T
{
uint32_t status[4];
uint32_t mat1;
uint32_t mat2;
uint32_t tmat;
};
typedef struct TINYMT32_T tinymt32_t;
void tinymt32_init(tinymt32_t *random, uint32_t seed);
/**
* @file tinymt32.c
*
* @brief Tiny Mersenne Twister only 127 bit internal state
*
* @author Mutsuo Saito (Hiroshima University)
* @author Makoto Matsumoto (The University of Tokyo)
*
* Copyright (C) 2011 Mutsuo Saito, Makoto Matsumoto,
* Hiroshima University and The University of Tokyo.
* All rights reserved.
*
* The 3-clause BSD License is applied to this software, see
* LICENSE.txt
*/
#define MIN_LOOP 8
#define PRE_LOOP 8
/**
* This function certificate the period of 2^127-1.
* @param random tinymt state vector.
*/
void period_certification(tinymt32_t *random)
{
if ((random->status[0] & TINYMT32_MASK) == 0 &&
random->status[1] == 0 &&
random->status[2] == 0 &&
random->status[3] == 0)
{
random->status[0] = 'T';
random->status[1] = 'I';
random->status[2] = 'N';
random->status[3] = 'Y';
}
}
/**
* This function changes internal state of tinymt32.
* Users should not call this function directly.
* @param random tinymt internal status
*/
void tinymt32_next_state(tinymt32_t *random)
{
uint32_t x;
uint32_t y;
y = random->status[3];
x = (random->status[0] & TINYMT32_MASK) ^ random->status[1] ^ random->status[2];
x ^= (x << TINYMT32_SH0);
y ^= (y >> TINYMT32_SH0) ^ x;
random->status[0] = random->status[1];
random->status[1] = random->status[2];
random->status[2] = x ^ (y << TINYMT32_SH1);
random->status[3] = y;
random->status[1] ^= -((int)(y & 1)) & random->mat1;
random->status[2] ^= -((int)(y & 1)) & random->mat2;
}
/**
* This function outputs 32-bit unsigned integer from internal state.
* Users should not call this function directly.
* @param random tinymt internal status
* @return 32-bit unsigned pseudorandom number
*/
uint32_t tinymt32_temper(tinymt32_t * random) {
uint32_t t0, t1;
t0 = random->status[3];
#if defined(LINEARITY_CHECK)
t1 = random->status[0]
^ (random->status[2] >> TINYMT32_SH8);
#else
t1 = random->status[0]
+ (random->status[2] >> TINYMT32_SH8);
#endif
t0 ^= t1;
t0 ^= -((int)(t1 & 1)) & random->tmat;
return t0;
}
/**
* This function outputs 32-bit unsigned integer from internal state.
* @param random tinymt internal status
* @return 32-bit unsigned integer r (0 <= r < 2^32)
*/
uint32_t tinymt32_generate_uint32(tinymt32_t * random) {
tinymt32_next_state(random);
return tinymt32_temper(random);
}
/**
* This function initializes the internal state array with a 32-bit
* unsigned integer seed.
* @param random tinymt state vector.
* @param seed a 32-bit unsigned integer used as a seed.
*/
void tinymt32_init(tinymt32_t *random, uint32_t seed)
{
random->status[0] = seed;
random->status[1] = random->mat1;
random->status[2] = random->mat2;
random->status[3] = random->tmat;
for (int i = 1; i < MIN_LOOP; i++)
{
random->status[i & 3] ^= i + (1812433253) * (random->status[(i - 1) & 3] ^ (random->status[(i - 1) & 3] >> 30));
}
period_certification(random);
for (int i = 0; i < PRE_LOOP; i++)
{
tinymt32_next_state(random);
}
}
#endif /* TINYMT32_H */
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <inttypes.h>
#include <stdbool.h>
typedef struct
{
DIR *input_dir;
char input_dir_path[PATH_MAX];
FILE *output_stream;
uint8_t nb_threads;
bool verbose;
} args_t;
void usage(char *prog_name)
{
fprintf(stderr, "USAGE:\n");
fprintf(stderr, " %s [OPTIONS] input_dir\n", prog_name);
fprintf(stderr, " input_dir: path to the directory containing the instance files with the encoded messages\n");
fprintf(stderr, " -f output_file: path to the output file containing all decoded messages\n");
fprintf(stderr, " -n n_threads (default: 4): set the number of computing threads that will be used to execute the RLC algorithm\n");
fprintf(stderr, " -v : enable debugging messages. If not set, no such messages will be displayed (except error messages on failure)\n");
}
int parse_args(args_t *args, int argc, char *argv[])
{
memset(args, 0, sizeof(args_t));
// Default values of the arguments
args->nb_threads = 4;
args->verbose = false;
args->output_stream = stdout;
int opt;
while ((opt = getopt(argc, argv, "n:vf:")) != -1)
{
switch (opt)
{
case 'n':
args->nb_threads = atoi(optarg);
if (args->nb_threads == 0)
{
fprintf(stderr, "The number of computing threads must be a positive integer, got: %s\n", optarg);
return -1;
}
break;
case 'v':
args->verbose = true;
break;
case 'f':
args->output_stream = fopen(optarg, "w");
if (args->output_stream == NULL)
{
fprintf(stderr, "Impossible to open the output file %s: %s\n", optarg, strerror(errno));
return -1;
}
break;
case '?':
usage(argv[0]);
return 1;
default:
usage(argv[0]);
}
}
if (optind == argc)
{
fprintf(stderr, "You must provide an input directory containing the instance files!\n");
return -1;
}
// Source: https://stackoverflow.com/questions/11736060/how-to-read-all-files-in-a-folder-using-c
if (NULL == (args->input_dir = opendir(argv[optind])))
{
fprintf(stderr, "Impossible to open the directory containing the input instance files %s: %s\n", argv[optind], strerror(errno));
return -1;
}
// The following line is not very secure... Ask Prof. Legay and/or wait for LINGI2144 for more information :-)
strcpy(args->input_dir_path, argv[optind++]);
return 0;
}
int main (int argc, char *argv[])
{
args_t args;
int err = parse_args(&args, argc, argv);
if (err == -1)
{
exit(EXIT_FAILURE);
}
else if (err == 1)
{
exit(EXIT_SUCCESS);
}
// The following lines (and every code already present in this skeleton) can be removed, it is just an example to show you how to use the program arguments
fprintf(stderr, "\tnumber of threads executing the RLC decoding algorithm in parallel: %" PRIu32 "\n", args.nb_threads);
fprintf(stderr, "\verbose mode: %s\n", args.verbose ? "enabled" : "disabled");
// This is an example of how to open the instance files of the input directory. You may move/edit it during the project
struct dirent *directory_entry;
FILE *input_file;
while ((directory_entry = readdir(args.input_dir)))
{
// Ignore parent and current directory
if (!strcmp(directory_entry->d_name, "."))
{
continue;
}
if (!strcmp(directory_entry->d_name, ".."))
{
continue;
}
// Add the directory path to the filename to open it
char full_path[PATH_MAX];
memset(full_path, 0, sizeof(char) * PATH_MAX);
strcpy(full_path, args.input_dir_path);
strcat(full_path, "/");
strcat(full_path, directory_entry->d_name);
input_file = fopen(full_path, "r");
if (input_file == NULL)
{
fprintf(stderr, "Failed to open the input file %s: %s\n", full_path, strerror(errno));
goto file_read_error;
}
if (args.verbose)
{
// This is a simple example of how to use the verbose mode
fprintf(stderr, "Successfully opened the file %s\n", full_path);
}
// TODO: parse the input binary file, decode the encoded message with RLC and write the output in the output stream following the statement
// Close this instance file
fclose(input_file);
}
// Close the input directory and the output file
err = closedir(args.input_dir);
if (err < 0)
{
fprintf(stderr, "Error while closing the input directory containing the instance files\n");
}
if (args.output_stream != stdout)
{
fclose(args.output_stream);
}
return 0;
file_read_error:
err = closedir(args.input_dir);
if (err < 0)
{
fprintf(stderr, "Error while closing the input directory containing the instance files\n");
}
if (args.output_stream != stdout)
{
fclose(args.output_stream);
}
exit(EXIT_FAILURE);
}
\ No newline at end of file
#include "../headers/tinymt32.h"
#include <CUnit/Basic.h>
void test_tinymt32_gen_42()
{
tinymt32_t prng;
memset(&prng, 0, sizeof(tinymt32_t));
prng.mat1 = 0x8f7011ee;
prng.mat2 = 0xfc78ff1f;
prng.tmat = 0x3793fdff;
tinymt32_init(&prng, 42);
uint8_t expected_res[] = {
171, 165, 55, 61, 69, 143, 152, 158, 168, 64, 5, 91, 215, 134, 85, 17, 29, 41, 122, 169, 75, 0, 84, 85, 231, 156, 156, 201, 100, 17, 96, 137, 207, 72, 15, 182, 183, 154, 222, 92, 143, 153, 105, 58, 56, 239, 37, 75, 40, 133, 106, 199, 200, 144, 136, 175, 219, 87, 100, 59, 62, 26, 208, 19, 194, 227, 31, 187, 176, 225, 189, 120, 189, 78, 13, 112, 12, 47, 80, 39, 149, 170, 184, 23, 227, 247, 99, 218, 163, 140, 58, 248, 171, 12, 99, 5, 78, 178, 155, 127, 138, 37, 12, 54, 142, 80, 96, 206, 24, 23, 127, 242, 106, 86, 135, 185, 255, 157, 1, 91, 213, 135, 75, 87, 216, 233, 11, 7, 235, 168, 31, 229, 231, 29, 51, 105, 163, 139, 156, 104, 119, 247, 158, 210, 158, 15, 120, 27, 237, 28, 31, 23, 212, 240, 44, 95, 201, 157, 168, 14, 20, 10, 222, 208, 34, 137, 217, 221, 111, 155, 4, 97, 119, 31, 132, 108, 62, 135, 76, 148, 18, 249, 169, 95, 162, 18, 68, 109, 50, 102, 216, 62, 204, 16, 160, 5, 205, 223, 75, 187, 239, 64, 64, 135, 1, 160, 141, 0, 132, 64, 59, 35, 24, 245, 245, 132, 126, 134, 41, 236, 88, 155, 108, 12, 100, 214, 87, 67, 172, 46, 71, 217, 97, 36, 37, 26, 87, 204, 17, 159, 91, 98, 64, 78, 87, 134, 18, 165, 233, 187, 71, 90, 71, 52, 203, 208, 145, 248, 42, 228, 214, 100, 243, 155, 183, 250, 126, 133, 243, 75, 236, 91, 177, 213, 20, 166, 198, 41, 112, 255, 152, 84, 32, 27, 76, 182, 47, 39, 226, 11, 226, 87, 162, 62, 51, 237, 107, 147, 230, 127, 149, 222, 94, 29, 194, 250, 60, 19, 215, 38, 187, 6, 254, 186, 227, 57, 158, 134, 230, 239, 185, 211, 91, 118, 187, 251, 221, 194, 54, 15, 90, 181, 219, 232, 180, 182, 94, 86, 43, 114, 132, 195, 222, 144, 118, 90, 135, 118, 204, 98, 116, 235, 198, 181, 229, 37, 208, 204, 55, 82, 123, 172, 185, 228, 57, 248, 248, 144, 154, 1, 172, 228, 233, 203, 137, 17, 4, 226, 23, 229, 119, 168, 223, 145, 203, 71, 54, 252, 34, 26, 36, 89, 35, 246, 2, 175, 191, 162, 60, 49, 171, 0, 2, 46, 248, 193, 114, 96, 174, 188, 240, 100, 238, 73, 142, 31, 14, 42, 161, 58, 193, 142, 116, 147, 9, 130, 60, 95, 155, 133, 200, 245, 250, 179, 29, 55, 208, 79, 110, 152, 214, 50, 115, 115, 224, 184, 74, 73, 209, 151, 127, 167, 22, 111, 127, 1, 93, 95, 196, 49, 17, 211, 71, 141, 45, 127, 2, 151, 190, 205, 1, 90, 76, 59, 176, 146, 33, 237, 71, 27, 227, 97, 70, 24, 87, 209, 237, 105, 8, 93, 81, 144, 171, 189, 16, 9, 34, 196, 201, 205, 179, 224, 34, 122, 118, 117, 199, 169, 109, 65, 180, 219, 186, 39, 221, 87, 151, 246, 243, 29, 172, 162, 233, 226, 96, 253, 140, 203, 181, 167, 49, 200, 72, 23, 232, 58, 45, 60, 14, 128, 78, 228, 204, 152, 80, 101, 105, 178, 44, 99, 218, 209, 97, 54, 5, 178, 246, 247, 232, 244, 130, 146, 67, 2, 219, 175, 142, 158, 174, 21, 26, 168, 203, 86, 242, 93, 239, 164, 26, 64, 150, 184, 71, 165, 152, 109, 163, 3, 42, 16, 213, 73, 160, 188, 74, 104, 219, 167, 197, 207, 227, 108, 3, 144, 150, 49, 174, 233, 119, 151, 161, 33, 242, 249, 94, 57, 101, 61, 241, 93, 86, 240, 245, 111, 87, 110, 230, 134, 205, 255, 37, 163, 156, 93, 150, 68, 131, 11, 8, 24, 46, 196, 151, 88, 89, 93, 241, 28, 194, 15, 176, 91, 60, 92, 126, 238, 251, 95, 153, 174, 108, 251, 118, 202, 216, 96, 207, 176, 7, 174, 1, 68, 116, 0, 252, 225, 248, 0, 85, 247, 222, 93, 57, 61, 224, 53, 210, 1, 150, 47, 165, 152, 185, 253, 6, 213, 158, 90, 29, 210, 130, 39, 11, 125, 1, 75, 16, 145, 203, 247, 8, 140, 72, 168, 199, 40, 59, 222, 183, 133, 102, 65, 161, 156, 163, 8, 27, 136, 185, 146, 44, 128, 64, 8, 73, 124, 241, 51, 157, 250, 110, 114, 213, 36, 153, 83, 24, 59, 38, 33, 9, 46, 232, 221, 136, 181, 11, 234, 247, 177, 206, 34, 232, 136, 10, 73, 190, 50, 198, 140, 132, 107, 83, 71, 146, 210, 198, 4, 104, 118, 246, 200, 168, 179, 136, 116, 215, 60, 71, 250, 225, 56, 199, 96, 181, 110, 138, 57, 115, 121, 123, 103, 136, 82, 21, 212, 236, 49, 26, 88, 239, 204, 147, 213, 9, 4, 112, 82, 12, 230, 193, 43, 10, 250, 172, 29, 101, 26, 101, 122, 96, 113, 164, 29, 43, 32, 5, 255, 124, 10, 43, 190, 207, 19, 210, 61, 143, 106, 229, 73, 63, 110, 38, 112, 243, 99, 121, 43, 148, 221, 141, 253, 90, 238, 58, 240, 172, 79, 8, 101, 139, 251, 216, 8, 67, 68, 164, 119, 160, 76, 54, 239, 1, 204, 208, 105, 93, 26, 199, 139, 231, 102, 183, 70, 168, 193, 179, 154, 83, 38, 11, 110, 174, 140, 14, 15, 35, 43, 217, 31, 174, 124, 237, 144, 185, 47, 48, 51, 170, 189, 156, 37, 234, 121, 10, 220, 17, 198, 81, 168, 93, 11, 240, 121, 97, 105, 188, 114, 133, 188, 20, 45, 130, 214, 241, 24, 100, 134, 112, 225, 203, 121, 141, 239, 31, 125, 13, 215, 251, 91, 165, 178, 160, 86, 152, 207, 185, 134, 21, 127, 101, 208, 217, 159, 134, 242, 35, 231, 123, 183, 172, 84, 111, 132, 74, 89, 1, 85, 217, 138, 206, 139, 222, 224, 126, 221, 96, 118, 138, 81
};
for (int i = 0; i < 1000; ++i)
{
uint8_t coef = (uint8_t)tinymt32_generate_uint32(&prng);
CU_ASSERT_EQUAL(coef, expected_res[i]);
}
}
int main()
{
CU_initialize_registry();
CU_pSuite suite = CU_add_suite("tinymt32", 0, 0);
CU_add_test(suite, "correct_coeffs", test_tinymt32_gen_42);
CU_basic_run_tests();
CU_basic_show_failures(CU_get_failure_list());
}
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter