Skip to content

Instantly share code, notes, and snippets.

@MasWag
Last active December 17, 2015 12:29
Show Gist options
  • Save MasWag/5610522 to your computer and use it in GitHub Desktop.
Save MasWag/5610522 to your computer and use it in GitHub Desktop.
test program of vector struct
#include <math.h>
typedef struct {
double x;
double y;
} vector;
vector make_vector(double x,double y);
vector plus_vector(vector a,vector b);
vector minus_vector(vector a,vector b);
double dot_product_vector(vector a,vector b);
double abs_vector(vector a);
vector scalar_multiple_vector(double a,vector b);
vector make_vector(double x,double y)
{
vector ret;
ret.x = x;
ret.y = y;
return ret;
}
vector plus_vector(vector a,vector b)
{
vector ret;
ret.x = a.x + b.x;
ret.y = a.y + b.y;
return ret;
}
vector minus_vector(vector a,vector b)
{
vector ret;
ret.x = a.x - b.x;
ret.y = a.y - b.y;
return ret;
}
double dot_product_vector(vector a,vector b)
{
double ret;
ret = a.x * b.x + a.y * b.y;
return ret;
}
double abs_vector(vector a)
{
double ret;
ret = sqrt(a.x * a.x + a.y * a.y);
return ret;
}
vector scalar_multiple_vector(double a,vector b)
{
b.x *= a;
b.y *= a;
return b;
}
#include <stdio.h>
#include <complex.h>
#include <time.h>
#include <stdlib.h>
typedef struct {
double x;
double y;
} vector;
extern vector make_vector(double x,double y);
extern vector plus_vector(vector a,vector b);
extern vector minus_vector(vector a,vector b);
extern double dot_product_vector(vector a,vector b);
extern double abs_vector(vector a);
extern vector scalar_multiple_vector(double a,vector b);
int main(void)
{
srand((unsigned int) time(NULL));
int t;
//start test of plus
for (t = 0;t < 100;t++)
{
double tmp[4] = { rand() / rand(),rand() / rand(),rand() / rand(),rand() / rand() };
complex c_a = tmp[0] + tmp[1] * I;
complex c_b = tmp[2] + tmp[3] * I;
vector v_a = make_vector( tmp[0] , tmp[1]);
vector v_b = make_vector( tmp[2] , tmp[3]);
complex c_c = c_a + c_b;
vector v_c = plus_vector(v_a,v_b);
if ( creal(c_c) != v_c.x || cimag(c_c) != v_c.y )
{
fprintf(stderr,"test of plus does not passed!!\n");
exit(1);
}
}
fprintf(stderr,"test of plus passed!!\n");
//start test of minus
for (t = 0;t < 100;t++)
{
double tmp[4] = { rand() / rand(),rand() / rand(),rand() / rand(),rand() / rand() };
complex c_a = tmp[0] + tmp[1] * I;
complex c_b = tmp[2] + tmp[3] * I;
vector v_a = make_vector( tmp[0] , tmp[1]);
vector v_b = make_vector( tmp[2] , tmp[3]);
complex c_c = c_a - c_b;
vector v_c = minus_vector(v_a,v_b);
if ( creal(c_c) != v_c.x || cimag(c_c) != v_c.y )
{
fprintf(stderr,"test of minus does not passed!!\n");
exit(1);
}
}
fprintf(stderr,"test of minus passed!!\n");
//start test of dot product
for (t = 0;t < 100;t++)
{
double tmp[4] = { rand() / rand(),rand() / rand(),rand() / rand(),rand() / rand() };
complex c_a = tmp[0] + tmp[1] * I;
complex c_b = tmp[2] + tmp[3] * I;
vector v_a = make_vector( tmp[0] , tmp[1]);
vector v_b = make_vector( tmp[2] , tmp[3]);
double v_dot = dot_product_vector(v_a,v_b);
if ( creal(c_a) * creal(c_b) + cimag(c_a) * cimag(c_b) != v_dot )
{
fprintf(stderr,"test of dot product does not passed!!\n");
exit(1);
}
}
fprintf(stderr,"test of dot product passed!!\n");
//start test of abs
for (t = 0;t < 100;t++)
{
double tmp[4] = { rand() / rand(),rand() / rand(),rand() / rand(),rand() / rand() };
complex c_a = tmp[0] + tmp[1] * I;
vector v_a = make_vector( tmp[0] , tmp[1]);
double v_abs = abs_vector(v_a);
if ( cabs(c_a) != v_abs )
{
fprintf(stderr,"test of abs does not passed!!\n");
exit(1);
}
}
fprintf(stderr,"test of abs passed!!\n");
//start test of scalar multiple
for (t = 0;t < 100;t++)
{
double tmp[4] = { rand() / rand(),rand() / rand(),rand() / rand(),rand() / rand() };
complex c_a = tmp[0] + tmp[1] * I;
vector v_a = make_vector( tmp[0] , tmp[1]);
vector v_scala = scalar_multiple_vector(tmp[2],v_a);
if ( creal(c_a) * tmp[2] != v_scala.x || cimag(c_a) * tmp[2] != v_scala.y )
{
fprintf(stderr,"test of scalar multiple does not passed!!\n");
exit(1);
}
}
fprintf(stderr,"test of scalar multiple passed!!\n");
fprintf(stderr,"all tests passed!!\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment