Author SHA1 Message Date
dimartinoma102424 5b467f42ae tests.cpp commited 2025-11-17 14:17:38 +01:00
dimartinoma102424 8023f4e866 CMAKE list is f***ed 2025-11-03 15:31:53 +01:00
dimartinoma102424 a8b2545b6c marco test files 2025-11-03 15:29:22 +01:00
dimartinoma102424 aa7f72820b Test Dokument erstellt 2025-11-03 15:08:56 +01:00
9 changed files with 184 additions and 8328 deletions
+2 -4
View File
@@ -19,8 +19,7 @@ set(SRC_FILES
${CMAKE_CURRENT_LIST_DIR}/src/main.cpp ${CMAKE_CURRENT_LIST_DIR}/src/main.cpp
${CMAKE_CURRENT_LIST_DIR}/src/gamecube.cpp ${CMAKE_CURRENT_LIST_DIR}/src/gamecube.cpp
src/gamematrix.cpp src/gamematrix.cpp
parallel/parallel_measurements.cpp docs/tests.cpp
parallel/matrizenmultiplikation.cpp
) )
##Tom: prev: ${CMAKE_CURRENT_LIST_DIR}/linux -> now /include ##Tom: prev: ${CMAKE_CURRENT_LIST_DIR}/linux -> now /include
@@ -39,7 +38,7 @@ target_include_directories(${EXECUTABLE_NAME} PRIVATE
) )
target_link_libraries(${EXECUTABLE_NAME} PRIVATE target_link_libraries(${EXECUTABLE_NAME} PRIVATE
# ${CMAKE_CURRENT_LIST_DIR}/${OS_NAME}/libgamematrix.a ${CMAKE_CURRENT_LIST_DIR}/${OS_NAME}/libgamematrix.a
${CMAKE_CURRENT_LIST_DIR}/${OS_NAME}/libraylib.a ${CMAKE_CURRENT_LIST_DIR}/${OS_NAME}/libraylib.a
) )
if (WIN32) if (WIN32)
@@ -52,4 +51,3 @@ if (APPLE)
target_link_libraries(Prog3B "-framework Cocoa") target_link_libraries(Prog3B "-framework Cocoa")
target_link_libraries(Prog3B "-framework OpenGL") target_link_libraries(Prog3B "-framework OpenGL")
endif() endif()
-74
View File
@@ -1,74 +0,0 @@
========================================================
Projekt: gamematrix (C++ Library)
Rolle: Architekt
Datei: design.txt
Datum: 03-November
Team: Thomas&Co
========================================================
# ----------------------------
# 1. Projektstruktur / Namespace
# ----------------------------
Namespace: Matrix3D
Ziel: Saubere Trennung der Bibliothek, Vermeidung von Namenskonflikten.
Beispiel:
namespace Matrix3D {
// Funktionen, ggf Klasse(n)
}
# ----------------------------
# 2. Datenstrukturen / Klassen
# ----------------------------
Listen Sie die Klassen oder Structs auf, die verwendet werden:
| Name | Typ | Beschreibung |
|--------|------------------------------------------|--------------|
| Vec3 | struct Vec3 | 3D-Vektor (x, y, z) |
| Mat4 | std::array<std::array<double,4>,4> | 4x4-Matrix (homogen) |
| ______ | ________ | ___________________ |
| ______ | ________ | ___________________ |
# ----------------------------
# 3. Operatoren / Templates
# ----------------------------
Welche Operatoren oder Templates sollen definiert werden?
- Templates für unterschiedliche Datentypen? ☐ Ja ☐ Nein
- Operatoren:
- Mat4 * Mat4
- Mat4 * Vec3
# ----------------------------
# 4. Funktionen / Schnittstellen
# ----------------------------
Liste der Funktionen mit Eingabe/Ausgabe und kurzer Beschreibung:
| Funktion | Eingabe | Ausgabe | Kurzbeschreibung |
|---------------|------------------------------------|-----------------------|----------------------------------------|
| matmul | Mat4 A, Mat4 B | Mat4 | Matrixmultiplikation 4x4 |
| translate | Vec3 pos | Mat4 | Verschiebungstransformation |
| rot3D | double angle_deg, char axis | Mat4 | Rotation um Achse x/y/z |
| identity (optional)| --- | Mat4 | Identitätsmatrix |
| _____________ | __________________________________ | ____________________ | ______________________________ |
# ----------------------------
# 5. Designentscheidungen / Hinweise
# ----------------------------
- Rückgabe der Matrizen per Wert oder Referenz? ___________
- Verwendung von std::array oder std::vector? ___________
- Homogene Koordinaten für Translation / Rotation (4x4)? ☐ Ja ☐ Nein
- Weitere Designüberlegungen: ___________________________
# ----------------------------
# 6. Deliverables / Milestones
# ----------------------------
- design.txt fertig und im Branch architect committed
- Übergabe an Entwickler für Implementierung
========================================================
Hinweis:
- Dieses Dokument dient als Grundlage für die Implementierung.
- Alle Designentscheidungen sollen klar nachvollziehbar sein.
========================================================
+137
View File
@@ -0,0 +1,137 @@
//
// Created by marco on 03.11.2025.
// Changeb by marco on 16.11.2025
#include <iostream>
#include <cmath>
#include "gamematrix.h"
using namespace Matrix3D;
bool nearlyEqual(double a, double b, double eps = 1e-6) {
return std::abs(a - b) < eps;
}
void test_matmul_identity() {
Mat4 A = identity();
Mat4 B = identity();
Mat4 R = matmul(A, B);
std::cout << "Test matmul: Identity * Identity -> Identity: ";
bool ok = true;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
double expected = (i==j ? 1.0 : 0.0);
if(!nearlyEqual(R[i][j], expected)) ok = false;
}
}
std::cout << (ok ? "OK\n" : "FAIL\n");
}
void test_matmul_example() {
Mat4 A = {{
{{1, 2, 3, 4}},
{{0, 1, 2, 3}},
{{0, 0, 1, 2}},
{{0, 0, 0, 1}}
}};
Mat4 B = {{
{{1, 0, 0, 0}},
{{1, 1, 0, 0}},
{{1, 1, 1, 0}},
{{1, 1, 1, 1}}
}};
Mat4 R = matmul(A, B);
std::cout << "Test matmul: Beispielmatrizen A * B: ";
// Erwartete Matrix mit Handrechnung:
Mat4 Expected = {{
{{1+2+3+4, 2+3+4, 3+4, 4}},
{{0+1+2+3, 1+2+3, 2+3, 3}},
{{0+0+1+2, 0+1+2, 1+2, 2}},
{{1, 1, 1, 1}}
}};
bool ok = true;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(!nearlyEqual(R[i][j], Expected[i][j])) ok = false;
}
}
std::cout << (ok ? "OK\n" : "FAIL\n");
}
void test_translate() {
Vec3 v{1,2,3};
Mat4 T = translate({5,-2,1});
Vec3 out = T * v;
std::cout << "Test translate (Vec3{1,2,3} + {5,-2,1}): ";
if(nearlyEqual(out.x, 6) && nearlyEqual(out.y, 0) && nearlyEqual(out.z, 4))
std::cout << "OK\n";
else
std::cout << "FAIL (" << out.x << "," << out.y << "," << out.z << ")\n";
}
void test_rotZ_90() {
Vec3 v{1,0,0};
Mat4 Rm = rot3D(90, 'z');
Vec3 out = Rm * v;
std::cout << "Test rot3D Z 90° (1,0,0 -> 0,1,0): ";
if(nearlyEqual(out.x, 0) && nearlyEqual(out.y, 1))
std::cout << "OK\n";
else
std::cout << "FAIL (" << out.x << "," << out.y << "," << out.z << ")\n";
}
void test_rotX_180() {
Vec3 v{0,1,0};
Mat4 Rm = rot3D(180, 'x');
Vec3 out = Rm * v;
std::cout << "Test rot3D X 180° (0,1,0 -> 0,-1,0): ";
if(nearlyEqual(out.y, -1))
std::cout << "OK\n";
else
std::cout << "FAIL (" << out.x << "," << out.y << "," << out.z << ")\n";
}
void test_rotY_270() {
Vec3 v{1,0,0};
Mat4 Rm = rot3D(270, 'y');
Vec3 out = Rm * v;
std::cout << "Test rot3D Y 270° (1,0,0 -> 0,0,-1): ";
if(nearlyEqual(out.x, 0) && nearlyEqual(out.z, -1))
std::cout << "OK\n";
else
std::cout << "FAIL (" << out.x << "," << out.y << "," << out.z << ")\n";
}
int main() {
std::cout << "===== Matrix3D Tests =====\n";
test_matmul_identity();
test_matmul_example();
test_translate();
test_rotZ_90();
test_rotX_180();
test_rotY_270();
std::cout << "==========================\n";
return 0;
}
+42
View File
@@ -0,0 +1,42 @@
========================================================
Projekt: gamematrix (C++ Library)
Rolle: Tester
Datei: tests.txt
Datum: ____________________
Team: ____________________
========================================================
# ----------------------------
# 1. Testplan Übersicht
# ----------------------------
Ziel: Überprüfung der Funktionen matmul(), translate(), rot3D().
| Funktion | Testfall | Eingabe | Erwartetes Ergebnis | Bemerkung |
|---------------|---------------------------|------------------------------|-----------------------------------|----------------------------|
| matmul | Identity * Identity | 4x4 Identity Matrizen | Identity | Basisfall |
| matmul | Beispielmatrizen | A=[[...]], B=[[...]] | C=[[...]] | Prüfen mit Handrechnung |
| translate | Verschiebung | Vec3 {1,2,3} | Matrix mit Translation 1,2,3 | Prüfen letzte Spalte |
| rot3D | Rotation Z 90° | angle_deg=90, axis='z' | (1,0,0) -> (0,1,0) | Prüfen Anwendung auf Vektor|
| rot3D | Rotation X 180° | angle_deg=180, axis='x' | (0,1,0) -> (0,-1,0) | Prüfen Anwendung auf Vektor|
| rot3D | Rotation Y 270° | angle_deg=270, axis='y' | (1,0,0) -> (0,0,-1) | Prüfen Anwendung auf Vektor|
# ----------------------------
# 2. Testdaten / Matrizen
# ----------------------------
- Matrizen für matmul: Identity, Beispiel A/B Matrizen
- Vektoren für translate: Vec3 {x, y, z}
- Vektoren für rot3D: Vec3 {1,0,0}, Vec3 {0,1,0}
# ----------------------------
# 3. Abnahmekriterien
# ----------------------------
- Alle Unit-Tests erfolgreich
- Keine Exceptions außer gewollt (z. B. ungültige Achse)
- Testbericht in tests.txt dokumentiert
========================================================
Hinweis:
- Diese Datei wird vom Tester gepflegt.
- Tester dokumentiert Input, Output, erwartetes Ergebnis und Erfolg/Fehler.
========================================================
-91
View File
@@ -1,91 +0,0 @@
#include <iostream>
#include <vector>
#include <chrono>
#include <omp.h>
// Funktion erzeugt zwei Matrizen A und B der Größe n x n mit Zufallswerten
void generateRandomMatrices(int n,
std::vector<std::vector<double>>& A,
std::vector<std::vector<double>>& B,
double min_val = 0.0,
double max_val = 10.0)
{
// Zufallsgenerator initialisieren
std::srand(static_cast<unsigned int>(std::time(nullptr)));
A.resize(n, std::vector<double>(n));
B.resize(n, std::vector<double>(n));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
{
double r1 = static_cast<double>(std::rand()) / RAND_MAX; // 0..1
double r2 = static_cast<double>(std::rand()) / RAND_MAX; // 0..1
A[i][j] = min_val + r1 * (max_val - min_val);
B[i][j] = min_val + r2 * (max_val - min_val);
}
}
std::vector<std::vector<double>> matmul_serial(
const std::vector<std::vector<double>>& A,
const std::vector<std::vector<double>>& B)
{
int n = A.size();
int m = B[0].size();
int p = B.size();
std::vector<std::vector<double>> C(n, std::vector<double>(m, 0.0));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
for (int k = 0; k < p; ++k)
C[i][j] += A[i][k] * B[k][j];
return C;
}
std::vector<std::vector<double>> matmul_parallel(
const std::vector<std::vector<double>>& A,
const std::vector<std::vector<double>>& B)
{
int n = A.size();
int m = B[0].size();
int p = B.size();
std::vector<std::vector<double>> C(n, std::vector<double>(m, 0.0));
#pragma omp parallel for
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
for (int k = 0; k < p; ++k)
C[i][j] += A[i][k] * B[k][j];
return C;
}
int main()
{
int N = 500;
std::vector<std::vector<double>> A, B;
generateRandomMatrices(N, A, B);
// Serielle Version
auto start = std::chrono::high_resolution_clock::now();
auto C_serial = matmul_serial(A, B);
auto end = std::chrono::high_resolution_clock::now();
std::cout << "Serielle Laufzeit: "
<< std::chrono::duration<double>(end - start).count()
<< " Sekunden\n";
// Parallele Version
start = std::chrono::high_resolution_clock::now();
auto C_parallel = matmul_parallel(A, B);
end = std::chrono::high_resolution_clock::now();
std::cout << "Parallele Laufzeit: "
<< std::chrono::duration<double>(end - start).count()
<< " Sekunden\n";
return 0;
}
File diff suppressed because it is too large Load Diff
-83
View File
@@ -1,83 +0,0 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <thread>
#include <chrono>
#include <string>
void readFile(const std::string& filename, std::vector<int>& data) {
std::ifstream file(filename);
if (!file) {
std::cerr << "Fehler: Datei '" << filename << "' konnte nicht geöffnet werden!\n";
return;
}
int v;
while (file >> v) data.push_back(v);
}
int countIncreasesSerial(const std::vector<int>& data) {
if (data.size() < 2) return 0;
int increases = 0;
for (size_t i = 1; i < data.size(); ++i)
if (data[i] > data[i - 1]) ++increases;
return increases;
}
int countIncreasesParallel(const std::vector<int>& messungen, int numThreads = 4) {
int totalComparisons = static_cast<int>(messungen.size()) - 1;
if (totalComparisons <= 0) return 0;
std::vector<int> results(numThreads, 0);
std::vector<std::thread> threads;
threads.reserve(numThreads);
int base = totalComparisons / numThreads;
int remainder = totalComparisons % numThreads;
int idx = 1;
for (int t = 0; t < numThreads; t++) {
int startIdx = idx;
int count = base + (t < remainder ? 1 : 0);
int endIdx = idx + count - 1;
idx = endIdx + 1;
if (count <= 0) {
results[t] = 0;
continue;
}
threads.emplace_back([&, t, startIdx, endIdx]() {
int localCount = 0;
for (int i = startIdx; i <= endIdx; ++i)
if (messungen[i] > messungen[i - 1])
++localCount;
results[t] = localCount;
});
}
for (auto& th : threads) th.join();
int total = 0;
for (int r : results) total += r;
return total;
}
int main() {
std::vector<int> messungen;
readFile("parallel/measurements.txt", messungen);
std::cout << "Gelesene Werte: " << messungen.size() << "\n";
auto t0 = std::chrono::steady_clock::now();
int s = countIncreasesSerial(messungen);
auto t1 = std::chrono::steady_clock::now();
auto p0 = std::chrono::steady_clock::now();
int p = countIncreasesParallel(messungen, 4);
auto p1 = std::chrono::steady_clock::now();
std::cout << "Seriell: " << s << " Dauer: " << std::chrono::duration<double>(t1 - t0).count() << "s\n";
std::cout << "Parallel: " << p << " Dauer: " << std::chrono::duration<double>(p1 - p0).count() << "s\n";
return 0;
}
+3 -76
View File
@@ -1,76 +1,3 @@
#include "gamematrix.h" //
#include <cmath> // Created by kmust on 03.11.2025.
//
namespace Matrix3D {
Mat4 identity() {
Mat4 m{};
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
m[i][j] = (i == j) ? 1.0 : 0.0;
}
}
return m;
}
Mat4 matmul(const Mat4& A, const Mat4& B) {
Mat4 R{};
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
double sum = 0.0;
for(int k = 0; k < 4; k++) {
sum += A[i][k] * B[k][j];
}
R[i][j] = sum;
}
}
return R;
}
Mat4 translate(const Vec3& pos) {
Mat4 t = identity();
t[0][3] = pos.x;
t[1][3] = pos.y;
t[2][3] = pos.z;
return t;
}
Mat4 rot3D(double angle_deg, char axis) {
Mat4 r = identity();
double rad = angle_deg * M_PI / 180.0;
double c = std::cos(rad);
double s = std::sin(rad);
switch(axis) {
case 'x': case 'X':
r[1][1] = c; r[1][2] = -s;
r[2][1] = s; r[2][2] = c;
break;
case 'y': case 'Y':
r[0][0] = c; r[0][2] = s;
r[2][0] = -s; r[2][2] = c;
break;
case 'z': case 'Z':
r[0][0] = c; r[0][1] = -s;
r[1][0] = s; r[1][1] = c;
break;
}
return r;
}
Vec3 operator*(const Mat4& M, const Vec3& v) {
Vec3 out;
out.x = M[0][0]*v.x + M[0][1]*v.y + M[0][2]*v.z + M[0][3];
out.y = M[1][0]*v.x + M[1][1]*v.y + M[1][2]*v.z + M[1][3];
out.z = M[2][0]*v.x + M[2][1]*v.y + M[2][2]*v.z + M[2][3];
return out;
}
Mat4 operator*(const Mat4& A, const Mat4& B) {
return matmul(A, B);
}
}
-1
View File
@@ -1,5 +1,4 @@
#include "gamecube.h" #include "gamecube.h"
#include "gamematrix.h"
#include <algorithm> #include <algorithm>
#include <ctime> #include <ctime>