Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
kmeans
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
dario.genga
kmeans
Commits
ff0f8b35
Commit
ff0f8b35
authored
2 years ago
by
dario.genga
Browse files
Options
Downloads
Patches
Plain Diff
Add tests for values_utils
parent
c1db1b96
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+1
-0
1 addition, 0 deletions
.gitignore
makefile
+4
-0
4 additions, 0 deletions
makefile
values_utils.c
+1
-1
1 addition, 1 deletion
values_utils.c
values_utils_test.c
+154
-0
154 additions, 0 deletions
values_utils_test.c
with
160 additions
and
1 deletion
.gitignore
+
1
−
0
View file @
ff0f8b35
...
...
@@ -79,6 +79,7 @@ dkms.conf
# Custom gitignore for project
main
tests
output_data.txt
cmake-build-debug
.idea
\ No newline at end of file
This diff is collapsed.
Click to expand it.
makefile
+
4
−
0
View file @
ff0f8b35
...
...
@@ -4,6 +4,10 @@ CC=gcc -Wall -Wextra -g
main
:
files_utils.o values_utils.o kmeans.o main.o
$(
CC
)
$^
-fsanitize
=
address
-fsanitize
=
leak
-o
$@
$(
LIB
)
run_tests
:
tests
./
$<
tests
:
values_utils_test.c values_utils.o
$(
CC
)
$^
-fsanitize
=
address
-fsanitize
=
leak
-o
$@
$(
LIB
)
files_utils.o
:
files_utils.c files_utils.h
$(
CC
)
-c
$<
$(
LIB
)
values_utils.o
:
values_utils.c values_utils.h
...
...
This diff is collapsed.
Click to expand it.
values_utils.c
+
1
−
1
View file @
ff0f8b35
...
...
@@ -41,7 +41,7 @@ float compute_chebyshev_distance(float* p1, float* p2, size_t dimensions) {
float
result
=
0
;
for
(
size_t
i
=
0
;
i
<
dimensions
;
i
++
)
{
in
t
abs_diff
=
fabs
(
p1
[
i
]
-
p2
[
i
]);
floa
t
abs_diff
=
fabs
(
p1
[
i
]
-
p2
[
i
]);
if
(
abs_diff
>
result
)
{
result
=
abs_diff
;
}
...
...
This diff is collapsed.
Click to expand it.
values_utils_test.c
0 → 100644
+
154
−
0
View file @
ff0f8b35
// Project : K-means
// Author : Dario GENGA
// Required include
#include
<stdio.h>
#include
<stdbool.h>
#include
<stdint.h>
// "Personal" include
#include
"values_utils.h"
#define FLOAT_EPSILON 1e-6
// For tests display
typedef
struct
_test_result
{
bool
passed
;
const
char
*
name
;
}
test_result
;
typedef
test_result
(
*
unit_test_t
)(
void
);
void
print_in_color
(
char
*
color
,
char
*
text
)
{
printf
(
"
\033
%s"
,
color
);
printf
(
"%s"
,
text
);
printf
(
"
\033
[0m"
);
}
void
print_in_red
(
char
*
text
)
{
print_in_color
(
"[0;31m"
,
text
);
}
void
print_in_green
(
char
*
text
)
{
print_in_color
(
"[0;32m"
,
text
);
}
// Custom utility methods
bool
float_eq
(
float
a
,
float
b
)
{
return
fabs
(
a
-
b
)
<
FLOAT_EPSILON
;
}
const
float
initial_x
[]
=
{
1
,
-
5
.
25
,
-
3
.
3
};
const
float
initial_y
[]
=
{
5
,
5
.
25
f
,
-
2
.
2
};
const
uint32_t
nb_tests
=
sizeof
(
initial_x
)
/
sizeof
(
float
);
// tests methods
test_result
t_compute_euclidean_distance_0
()
{
bool
passed
=
true
;
float
expected_one_dimension_val
[
3
]
=
{
4
,
10
.
5
,
1
.
1
};
float
expected_val
=
11
.
289818
;
float
t_val
=
0
;
// Multiple one dimension tests
for
(
uint32_t
i
=
0
;
i
<
nb_tests
;
i
++
)
{
t_val
=
compute_euclidean_distance
((
float
*
)
&
(
initial_x
)[
i
],
(
float
*
)
&
(
initial_y
)[
i
],
1
);
if
(
!
float_eq
(
t_val
,
expected_one_dimension_val
[
i
]))
{
passed
=
false
;
break
;
}
}
// Test with 3 dimensions
t_val
=
compute_euclidean_distance
((
float
*
)
initial_x
,
(
float
*
)
initial_y
,
3
);
if
(
!
float_eq
(
t_val
,
expected_val
))
{
passed
=
false
;
}
return
(
test_result
)
{.
passed
=
passed
,
.
name
=
"Test compute_euclidean_distance"
};
}
test_result
t_compute_manhattan_distance_0
()
{
bool
passed
=
true
;
float
expected_one_dimension_val
[
3
]
=
{
4
,
10
.
5
,
1
.
1
};
float
expected_val
=
15
.
6
;
float
t_val
=
0
;
// Multiple one dimension tests
for
(
uint32_t
i
=
0
;
i
<
nb_tests
;
i
++
)
{
t_val
=
compute_manhattan_distance
((
float
*
)
&
(
initial_x
)[
i
],
(
float
*
)
&
(
initial_y
)[
i
],
1
);
if
(
!
float_eq
(
t_val
,
expected_one_dimension_val
[
i
]))
{
passed
=
false
;
break
;
}
}
// Test with 3 dimensions
t_val
=
compute_manhattan_distance
((
float
*
)
initial_x
,
(
float
*
)
initial_y
,
3
);
if
(
!
float_eq
(
t_val
,
expected_val
))
{
passed
=
false
;
}
return
(
test_result
)
{.
passed
=
passed
,
.
name
=
"Test compute_euclidean_distance"
};
}
test_result
t_compute_chebyshev_distance_0
()
{
bool
passed
=
true
;
float
expected_one_dimension_val
[
3
]
=
{
4
,
10
.
5
,
1
.
1
};
float
expected_val
=
10
.
5
;
float
t_val
=
0
;
// Multiple one dimension tests
for
(
uint32_t
i
=
0
;
i
<
nb_tests
;
i
++
)
{
t_val
=
compute_chebyshev_distance
((
float
*
)
&
(
initial_x
)[
i
],
(
float
*
)
&
(
initial_y
)[
i
],
1
);
if
(
!
float_eq
(
t_val
,
expected_one_dimension_val
[
i
]))
{
passed
=
false
;
break
;
}
}
// Test with 3 dimensions
t_val
=
compute_chebyshev_distance
((
float
*
)
initial_x
,
(
float
*
)
initial_y
,
3
);
if
(
!
float_eq
(
t_val
,
expected_val
))
{
passed
=
false
;
}
return
(
test_result
)
{.
passed
=
passed
,
.
name
=
"Test compute_euclidean_distance"
};
}
// Add or remove the test function name here
const
unit_test_t
tests
[]
=
{
t_compute_euclidean_distance_0
,
t_compute_manhattan_distance_0
,
t_compute_chebyshev_distance_0
};
int
main
()
{
uint32_t
nb_tests
=
sizeof
(
tests
)
/
sizeof
(
unit_test_t
);
char
message
[
256
];
bool
all_passed
=
true
;
for
(
uint32_t
i
=
0
;
i
<
nb_tests
;
i
++
)
{
printf
(
"Running test n°%d: ...
\n
"
,
i
);
test_result
r
=
tests
[
i
]();
if
(
r
.
passed
)
{
sprintf
(
message
,
"
\t
- %s : OK"
,
r
.
name
);
print_in_green
(
message
);
}
else
{
all_passed
=
false
;
sprintf
(
message
,
"
\t
- %s : FAILED"
,
r
.
name
);
print_in_red
(
message
);
}
printf
(
"
\n
"
);
}
if
(
all_passed
)
print_in_green
(
"
\n
Tests suite result : OK
\n
"
);
else
print_in_red
(
"
\n
Tests suite result : FAILED
\n
"
);
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment