Skip to content

Instantly share code, notes, and snippets.

@sumantro93
Created August 13, 2023 04:09
Show Gist options
  • Save sumantro93/9774eff8695897f4df1ad92bc3b9705d to your computer and use it in GitHub Desktop.
Save sumantro93/9774eff8695897f4df1ad92bc3b9705d to your computer and use it in GitHub Desktop.
Boost C++ test case for libs
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/thread.hpp>
#include <boost/atomic.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/null.hpp>
// Atomic counter
boost::atomic<int> counter(0);
// A simple thread function
void thread_function() {
for(int i = 0; i < 10000; ++i) {
++counter;
}
}
int main() {
// Using boost::filesystem to create a directory
boost::filesystem::path dir("./boost_test_dir");
if(boost::filesystem::create_directory(dir)) {
std::cout << "Directory created successfully!" << std::endl;
} else {
std::cout << "Directory already exists or failed to create!" << std::endl;
}
// Using boost::thread to spawn two threads
boost::thread t1(thread_function);
boost::thread t2(thread_function);
t1.join();
t2.join();
std::cout << "Counter after two threads: " << counter.load() << std::endl;
// Using boost::iostreams to create a null output stream
boost::iostreams::stream<boost::iostreams::null_sink> nullOstream((boost::iostreams::null_sink()));
nullOstream << "This message goes nowhere!";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment