Skip to content

Commit

Permalink
tests ok. df cols not inorder
Browse files Browse the repository at this point in the history
  • Loading branch information
SermetPekin committed Dec 8, 2024
1 parent 20548a6 commit c30fd6a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 20 deletions.
1 change: 1 addition & 0 deletions include/micrograd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ THE SOFTWARE.
#include "series.hpp"
#include "dataframe.hpp"
#include "dataframe_utils.hpp"
#include "sp_testing_utils.hpp"

#include "value.hpp"
#include "iris.hpp"
Expand Down
37 changes: 37 additions & 0 deletions include/sp_testing_utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef SPTEST_HPP
#define SPTEST_HPP

#include <vector>
#include <algorithm>
#include <fstream>

namespace sptest
{

static inline void create_temp_csv(const std::string &filename, const std::string &content)
{
std::ofstream file(filename);
// ASSERT_TRUE(file.is_open());
file << content;
file.close();
}

template <typename T, typename U>
static inline bool contains(const std::vector<T> &vec, const U &item)
{

try
{
T x = static_cast<T>(item);
return std::find(vec.begin(), vec.end(), x) != vec.end();
}
catch (...)
{

return false;
}
}

}

#endif // SPTEST_HPP
49 changes: 29 additions & 20 deletions tests/test_dataframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@
#include <fstream>
#include "micrograd.hpp"
#include "dataframe.hpp"
#include "sp_testing_utils.hpp"

#include <vector>
#include <algorithm>
using namespace microgradCpp;

using namespace sptest ;



// Helper function to create a temporary CSV file
void create_temp_csv(const std::string& filename, const std::string& content) {
std::ofstream file(filename);
ASSERT_TRUE(file.is_open());
file << content;
file.close();
}
// void create_temp_csv(const std::string &filename, const std::string &content)
// {
// std::ofstream file(filename);
// // ASSERT_TRUE(file.is_open());
// file << content;
// file.close();
// }

TEST(DataFrameTest, ValidateDataFrameContents) {
TEST(DataFrameTest, ValidateDataFrameContents)
{
microgradCpp::DataFrame df;

std::string temp_file = "temp_iris.csv";
Expand Down Expand Up @@ -42,9 +51,9 @@ TEST(DataFrameTest, ValidateDataFrameContents) {
std::remove(temp_file.c_str());
}


// Test encoding a categorical column in a DataFrame
TEST(DataFrameTest, EncodeCategoricalColumn) {
TEST(DataFrameTest, EncodeCategoricalColumn)
{
std::string temp_file = "temp_species.csv";
std::string csv_content =
"species\n"
Expand All @@ -55,7 +64,7 @@ TEST(DataFrameTest, EncodeCategoricalColumn) {

create_temp_csv(temp_file, csv_content);

microgradCpp::DataFrame df;
microgradCpp::DataFrame df;
df.from_csv(temp_file);

df.encode_column("species");
Expand All @@ -73,7 +82,8 @@ TEST(DataFrameTest, EncodeCategoricalColumn) {
}

// Test loading a CSV and checking inferred column types
TEST(DataFrameTest, CheckInferredTypes) {
TEST(DataFrameTest, CheckInferredTypes)
{
std::string temp_file = "temp_mixed.csv";
std::string csv_content =
"col1,col2,col3\n"
Expand All @@ -92,8 +102,6 @@ TEST(DataFrameTest, CheckInferredTypes) {
std::remove(temp_file.c_str());
}



// Test to check loading and saving an Iris-like dataset
// TEST(DataFrameTest, LoadAndSaveCSV) {
// std::string temp_file = "temp_iris.csv";
Expand Down Expand Up @@ -151,7 +159,6 @@ TEST(DataFrameTest, CheckInferredTypes) {
// std::remove(output_file.c_str());
// }


// TEST(DataFrameTest, LoadCSV) {
// std::string temp_file = "temp_iris.csv";
// std::string csv_content =
Expand All @@ -173,7 +180,8 @@ TEST(DataFrameTest, CheckInferredTypes) {
// std::remove(temp_file.c_str());
// }

TEST(DataFrameTest, LoadCSVParts) {
TEST(DataFrameTest, LoadCSVParts)
{
std::string temp_file = "temp_iris.csv";
std::string csv_content =
"sepal_length,sepal_width,petal_length,petal_width,species\n"
Expand All @@ -188,13 +196,17 @@ TEST(DataFrameTest, LoadCSVParts) {

auto columns = df.get_column_names();
EXPECT_EQ(columns.size(), 5);
EXPECT_EQ(columns[0], "sepal_length");

// EXPECT_THAT(columns, ::testing::Contains("sepal_length"));

ASSERT_TRUE(sptest::contains(columns, "sepal_length")) << "'sepal_length' not found in columns";

// EXPECT_EQ(columns[0], "sepal_length");
// EXPECT_EQ(columns[4], "species");

// std::remove(temp_file.c_str());
}


// TEST(DataFrameTest, SaveCSV) {
// microgradCpp::DataFrame df;

Expand Down Expand Up @@ -232,6 +244,3 @@ TEST(DataFrameTest, LoadCSVParts) {
// std::remove(temp_file.c_str());
// std::remove(output_file.c_str());
// }



0 comments on commit c30fd6a

Please sign in to comment.