#define BOOST_TEST_MODULE TestProgramCache
#include <boost/test/unit_test.hpp>
#include <boost/compute/system.hpp>
#include <boost/compute/utility/program_cache.hpp>
#include "context_setup.hpp"
namespace compute = boost::compute;
BOOST_AUTO_TEST_CASE(setup)
{
compute::context ctx = context;
boost::shared_ptr<compute::program_cache> cache =
compute::program_cache::get_global_cache(ctx);
BOOST_CHECK(cache->get(std::string()) == boost::none);
BOOST_CHECK(cache->get("nonexistant") == boost::none);
const char p1_source[] =
"__kernel void add(__global int *a, int x)\n"
"{\n"
" a[get_global_id(0)] += x;\n"
"}\n";
compute::program p1 =
compute::program::create_with_source(p1_source, ctx);
p1.build();
cache->insert("p1", p1);
BOOST_CHECK(cache->get("p1") == p1);
compute::context ctx_copy = ctx;
BOOST_CHECK(ctx_copy.get() == ctx.get());
boost::shared_ptr<compute::program_cache> cache_copy =
compute::program_cache::get_global_cache(ctx_copy);
BOOST_CHECK(cache_copy == cache);
BOOST_CHECK(cache_copy->get("p1") == p1);
}
BOOST_AUTO_TEST_CASE(evict)
{
compute::program_cache cache(4);
cache.insert("a", compute::program());
cache.insert("b", compute::program());
cache.insert("c", compute::program());
cache.insert("d", compute::program());
BOOST_CHECK(cache.get("a") != boost::none);
BOOST_CHECK(cache.get("b") != boost::none);
BOOST_CHECK(cache.get("c") != boost::none);
BOOST_CHECK(cache.get("d") != boost::none);
cache.insert("e", compute::program());
BOOST_CHECK(cache.get("a") == boost::none);
BOOST_CHECK(cache.get("b") != boost::none);
BOOST_CHECK(cache.get("c") != boost::none);
BOOST_CHECK(cache.get("d") != boost::none);
BOOST_CHECK(cache.get("e") != boost::none);
cache.clear();
BOOST_CHECK(cache.get("a") == boost::none);
BOOST_CHECK(cache.get("b") == boost::none);
BOOST_CHECK(cache.get("c") == boost::none);
BOOST_CHECK(cache.get("d") == boost::none);
BOOST_CHECK(cache.get("e") == boost::none);
}
BOOST_AUTO_TEST_SUITE_END()