Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_emission #63

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions cxx/emissions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ cc_library(
],
)

cc_library(
name = "get_emission",
srcs = ["get_emission.cc"],
hdrs = ["get_emission.hh"],
visibility = ["//:__subpackages__"],
deps = [
":bitflip",
":gaussian",
":simple_string",
":sometimes",
],
)

cc_library(
name = "bitflip",
srcs = ["bitflip.hh"],
Expand Down Expand Up @@ -46,6 +59,16 @@ cc_library(
],
)

cc_test(
name = "get_emission_test",
srcs = ["get_emission_test.cc"],
deps = [
":get_emission",
"@boost//:algorithm",
"@boost//:test",
],
)

cc_test(
name = "bitflip_test",
srcs = ["bitflip_test.cc"],
Expand Down
24 changes: 24 additions & 0 deletions cxx/emissions/get_emission.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2024
// See LICENSE.txt

#include "emissions/get_emission.hh"

#include <cassert>

#include "emissions/bitflip.hh"
#include "emissions/gaussian.hh"
#include "emissions/simple_string.hh"

EmissionVariant get_emission(const std::string& emission_name) {
if (emission_name == "gaussian") {
return new GaussianEmission();
} else if (emission_name == "simple_string") {
return new SimpleStringEmission();
} else if (emission_name == "sometimes_gaussian") {
return new SometimesGaussian();
} else if (emission_name == "sometimes_bitflip") {
return new SometimesBitFlip();
}
printf("Unknown emission name %s\n", emission_name.c_str());
assert(false);
}
20 changes: 20 additions & 0 deletions cxx/emissions/get_emission.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2024
// See LICENSE.txt

#pragma once

#include <string>
#include <variant>

#include "emissions/sometimes.hh"

class BitFlip;
class GaussianEmission;
class SimpleStringEmission;
using SometimesBitFlip = Sometimes<BitFlip, bool>;
using SometimesGaussian = Sometimes<GaussianEmission, double>;

using EmissionVariant = std::variant<GaussianEmission*, SometimesGaussian*,
SometimesBitFlip*, SimpleStringEmission*>;

EmissionVariant get_emission(const std::string& emission_name);
41 changes: 41 additions & 0 deletions cxx/emissions/get_emission_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#define BOOST_TEST_MODULE test get_emission

#include "emissions/get_emission.hh"

#include <boost/test/included/unit_test.hpp>

#include "emissions/bitflip.hh"
#include "emissions/gaussian.hh"
#include "emissions/simple_string.hh"

BOOST_AUTO_TEST_CASE(test_get_emission_gaussian) {
EmissionVariant ev = get_emission("gaussian");
GaussianEmission* ge = std::get<GaussianEmission*>(ev);

ge->incorporate(std::make_pair<double, double>(2.0, 2.1));
BOOST_TEST(ge->N == 1);
}

BOOST_AUTO_TEST_CASE(test_get_emission_simple_string) {
EmissionVariant ev = get_emission("simple_string");
SimpleStringEmission* sse = std::get<SimpleStringEmission*>(ev);

sse->incorporate(std::make_pair<std::string, std::string>("hello", "hi"));
BOOST_TEST(sse->N == 1);
}

BOOST_AUTO_TEST_CASE(test_get_emission_sometimes_gaussian) {
EmissionVariant ev = get_emission("sometimes_gaussian");
SometimesGaussian* sg = std::get<SometimesGaussian*>(ev);

sg->incorporate(std::make_pair<double, double>(2.0, 2.1));
BOOST_TEST(sg->N == 1);
}

BOOST_AUTO_TEST_CASE(test_get_emission_sometimes_bitflip) {
EmissionVariant ev = get_emission("sometimes_bitflip");
SometimesBitFlip* sbf = std::get<SometimesBitFlip*>(ev);

sbf->incorporate(std::make_pair<bool, bool>(true, true));
BOOST_TEST(sbf->N == 1);
}