Unverified Commit a762a853 authored by Guillaume Anciaux's avatar Guillaume Anciaux
Browse files

organizing the code snippets

parent 33b7b175
cmake_minimum_required (VERSION 2.6)
project (GeneratedFromCodeGenerator)
add_executable(hello
hello.cpp
)
#include <algorithm>
#include <cmath>
#include <exception>
#include <gtest/gtest.h>
#include <numeric>
#include <vector>
struct Particle {
double position[3];
double velocity[3];
double force[3];
};
struct System {
std::vector<Particle> particles;
};
// Fixture class
class SystemTest : public ::testing::Test {
protected:
void SetUp() override {
// push 10 particles
system.particles.resize(10);
}
void TearDown() override {
// clear particles
system.particles.clear();
}
System system;
};
// Fixture test
TEST_F(SystemTest, IsNotEmpty) {
// check the size of container
EXPECT_EQ(system.particles.size(), 10);
}
\ No newline at end of file
import pytest
import numpy as np
class Particle:
def __init__(self):
position = np.zeros(3)
velocity = np.zeros(3)
force = np.zeros(3)
class System:
particles = []
@pytest.fixture
def SystemTest():
# SetUp
System.particles = [Particle() for i in range(0, 10)]
yield
# TearDown
System.particles = []
def test_is_not_empty(SystemTest):
assert len(System.particles) == 10
#include <cmath>
#include <exception>
struct NegativeException : public std::exception {};
double mysqrt(double x) {
if (x < 0.)
throw NegativeException();
return std::sqrt(x);
}
#include <gtest/gtest.h>
// MACRO function "TEST" from Google Test
// defines a test and a context
TEST(MysqrtTest, positive_integers) {
EXPECT_EQ(2, mysqrt(4));
EXPECT_EQ(4, mysqrt(16));
EXPECT_EQ(3, mysqrt(9));
}
TEST(MysqrtTest, positive_doubles) {
ASSERT_NEAR(std::sqrt(2), mysqrt(2), 1e-15);
ASSERT_NEAR(std::sqrt(3), mysqrt(3), 1e-15);
}
TEST(MysqrtTest, negative) {
// check that exception is thrown
ASSERT_THROW(mysqrt(-1), NegativeException);
}
\ No newline at end of file
import pytest
import math
class NegativeException(Exception):
pass
def mysqrt(x):
if (x < 0.):
raise NegativeException();
return math.sqrt(x);
def test_mysqrt_int():
assert mysqrt(4) == 2
def test_mysqrt_double():
assert mysqrt(2) == math.sqrt(2)
def test_mysqrt_negative():
with pytest.raises(NegativeException):
mysqrt(-1)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment