Skip to content
Snippets Groups Projects
Commit b1bbc2c7 authored by tanguy.cavagna's avatar tanguy.cavagna :desktop:
Browse files

Merge branch 'hotfix/rename-var-name' into 'master'

Hotfix : renamed vector variable name to vec2

See merge request physics/electric-field!3
parents c5da2377 26d6a4dc
No related branches found
No related tags found
1 merge request!3Hotfix : renamed vector variable name to vec2
// cmocka includes
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <cmocka.h>
// vector include
#include <stdio.h>
#include <stdbool.h>
#include "vector.h"
//* ====================
//* Tests descriptions
//* ====================
static void vector_initialization() {
vec2 v = {2, 3};
assert_non_null(&v);
}
static void vector_addition() {
vec2 v1 = {2, 5};
vec2 v2 = {3, 7};
vec2 v3;
vec2 target = {5, 12};
v3 = add_vector(v1, v2);
assert_memory_equal(&v3, &target, sizeof(target));
}
static void vector_substraction() {
vec2 v1 = {2, 5};
vec2 v2 = {3, 7};
vec2 v3;
vec2 target = {-1, -2};
v3 = sub_vector(v1, v2);
assert_memory_equal(&v3, &target, sizeof(target));
}
static void vector_multiplication_scalar() {
vec2 v1 = {2, 5};
int scal = 2;
vec2 v2;
vec2 target = {4, 10};
v2 = mult_by_scalar(v1, scal);
assert_memory_equal(&v2, &target, sizeof(target));
}
static void vector_to_polar_convertion() {
vec2 c_v = {84.85, -84.85};
polarVector p_v;
polarVector target = {119.996, -45};
p_v = to_polar(c_v);
assert_float_equal(p_v.a, target.a, 1e-3);
assert_float_equal(p_v.f, target.f, 1e-3);
}
static void vector_to_cartesian_convertion() {
polarVector p_v = {200, 60};
vec2 c_v;
vec2 target = {100, 173.205};
c_v = to_cartesian(p_v);
assert_float_equal(c_v.x, target.x, 1e-3);
assert_float_equal(c_v.y, target.y, 1e-3);
}
//* ====================
//* Run test
//* ====================
int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(vector_initialization),
cmocka_unit_test(vector_addition),
cmocka_unit_test(vector_substraction),
cmocka_unit_test(vector_multiplication_scalar),
cmocka_unit_test(vector_to_polar_convertion),
cmocka_unit_test(vector_to_cartesian_convertion),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
\ No newline at end of file
......@@ -16,8 +16,8 @@
\param v1 First vector to add
\param v2 Second vector to add
*/
vector add_vector(vector v1, vector v2) {
vector resultVector;
vec2 add_vector(vec2 v1, vec2 v2) {
vec2 resultVector;
double finalX = v1.x + v2.x;
double finalY = v1.y + v2.y;
......@@ -33,7 +33,7 @@ vector add_vector(vector v1, vector v2) {
\param v1 First vector
\param v2 Vector we want to substract to the first
*/
vector sub_vector(vector v1, vector v2) {
vec2 sub_vector(vec2 v1, vec2 v2) {
// First we reverse the direction of the vector we want to substract
v2.x *= -1;
v2.y *= -1;
......@@ -47,8 +47,8 @@ vector sub_vector(vector v1, vector v2) {
\param v1 Vector to multiply
\param scalar Scalar to use for the multiplication
*/
vector mult_by_scalar(vector v1, int scalar) {
vector resultVector = {v1.x * scalar, v1.y * scalar};
vec2 mult_by_scalar(vec2 v1, int scalar) {
vec2 resultVector = {v1.x * scalar, v1.y * scalar};
return resultVector;
}
......@@ -57,7 +57,7 @@ vector mult_by_scalar(vector v1, int scalar) {
\brief Get the norm of a given vector
\param v1 Vector to multiply
*/
double norm(vector v1) {
double norm(vec2 v1) {
double vectorNorm;
vectorNorm = sqrt(pow(v1.x, 2) + pow(v1.y, 2));
......@@ -70,7 +70,7 @@ double norm(vector v1) {
\param v1 First vector to multiply
\param v2 Second vector to multiply
*/
double dot_multiply(vector v1, vector v2) {
double dot_multiply(vec2 v1, vec2 v2) {
return (v1.x * v2.x) + (v1.y * v2.y);
}
......@@ -78,7 +78,7 @@ double dot_multiply(vector v1, vector v2) {
\brief Convert a Cartesian vector to a Polar vector
\param v1 Cartesian vector
*/
polarVector to_polar(vector v1) {
polarVector to_polar(vec2 v1) {
polarVector p_vector;
p_vector.f = norm(v1);
......@@ -91,8 +91,8 @@ polarVector to_polar(vector v1) {
\brief Convert a Polar vector to a Cartesian vector
\param v1 Polar vector
*/
vector to_cartesian(polarVector v1) {
vector c_vector;
vec2 to_cartesian(polarVector v1) {
vec2 c_vector;
// The cos work with radian angle. Convert degree to radiant inside the cos function to have the correct force
c_vector.x = v1.f * cos(v1.a * (M_PI / 180.0));
......
......@@ -11,7 +11,7 @@
typedef struct {
double x;
double y;
} vector;
} vec2;
/**
* @brief Polar vector used for representation only
......@@ -21,13 +21,13 @@ typedef struct {
double a; /* Angle of the vector */
} polarVector;
vector add_vector(vector v1, vector v2); /* Add a vector to another and return the result as a vector */
vector sub_vector(vector v1, vector v2); /* Substract a vector to another vector and return the result as a vector */
vector mult_by_scalar(vector v1, int scalar); /* Multiply a vector by a scalar */
double dot_multiply(vector v1, vector v2); /* Multiply two vectors with the dot product formula */
double norm(vector v1); /* Get the norm of a given vector */
polarVector to_polar(vector v1); /* Convert a Cartesian vector to a Polar vector */
vector to_cartesian(polarVector v1); /* Convert a Polar vector to a Cartesian vector */
vec2 add_vector(vec2 v1, vec2 v2); /* Add a vector to another and return the result as a vector */
vec2 sub_vector(vec2 v1, vec2 v2); /* Substract a vector to another vector and return the result as a vector */
vec2 mult_by_scalar(vec2 v1, int scalar); /* Multiply a vector by a scalar */
double dot_multiply(vec2 v1, vec2 v2); /* Multiply two vectors with the dot product formula */
double norm(vec2 v1); /* Get the norm of a given vector */
polarVector to_polar(vec2 v1); /* Convert a Cartesian vector to a Polar vector */
vec2 to_cartesian(polarVector v1); /* Convert a Polar vector to a Cartesian vector */
/* For the moment, the cross product hasn't be inplemented because it only work with vectors on 3 dimentionals plan. */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment