Skip to content

Commit

Permalink
Merge pull request #63 from probcomp/061724-thomaswc-getemission
Browse files Browse the repository at this point in the history
Add get_emission
  • Loading branch information
ThomasColthurst authored Jun 17, 2024
2 parents c43f065 + a1b6cbc commit 1d7e48f
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
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);
}

0 comments on commit 1d7e48f

Please sign in to comment.