Skip to content
Snippets Groups Projects
Select Git revision
  • 74a8ef5d0ab17dfdd3b2c08da839498dfe9caff4
  • master default protected
2 results

ExampleTests.cpp

Blame
  • ExampleTests.cpp 3.41 KiB
    #include "gtest/gtest.h"
    
    // this is how we include our c code in the test
    extern "C" {
        #include "../include/compt_frequence.h"
    }
    TEST(ExampleTests, test_init_frequency_counter) {
        FrequencyCounter counter;
        init_frequency_counter(&counter);
        EXPECT_EQ(0, counter.count);
        EXPECT_TRUE(counter.words==NULL);
    }
    
    
    TEST(ExampleTests, test_deleteFirstChar) {
        char str[] = "hello";
        deleteFirstChar(str);
        EXPECT_STREQ("ello", str);
    }
    
    
    TEST(ExampleTests, test_read_and_count_words) {
        FrequencyCounter counter;
        init_frequency_counter(&counter);
        char cwd[1024];
        if (getcwd(cwd, sizeof(cwd)) != NULL) {
            printf("Current working directory: %s\n", cwd);
        }
        read_and_count_words(&counter, "test.txt");
        EXPECT_EQ(3, counter.count);
        EXPECT_STREQ("hello", counter.words[0].word);
        EXPECT_EQ(3, counter.words[0].frequency);
        EXPECT_STREQ("world", counter.words[1].word);
        EXPECT_EQ(1, counter.words[1].frequency);
        EXPECT_STREQ("foo", counter.words[2].word);
        EXPECT_EQ(2, counter.words[2].frequency);
        cleanup_frequency_counter(&counter);
    
    }
    
    TEST(ExampleTests, test_sort_words_by_frequency) {
        FrequencyCounter counter;
        init_frequency_counter(&counter);
        read_and_count_words(&counter, "test.txt");
        sort_words_by_frequency(&counter);
        EXPECT_EQ(3, counter.count);
        EXPECT_STREQ("hello", counter.words[0].word);
        EXPECT_STREQ("foo", counter.words[1].word);
        EXPECT_STREQ("world", counter.words[2].word);
        cleanup_frequency_counter(&counter);
    }
    
    TEST(ExampleTests, test_write_results) {
        FrequencyCounter counter;
        init_frequency_counter(&counter);
        read_and_count_words(&counter, "test.txt");
        sort_words_by_frequency(&counter);
        write_results(&counter, "output.txt");
        cleanup_frequency_counter(&counter);
        // check the output file
        FILE *f = fopen("output.txt", "r");
        char line[100];
        fgets(line, 100, f);
        EXPECT_STREQ("hello 3\n", line);
        fgets(line, 100, f);
        EXPECT_STREQ("foo 2\n", line);
        fgets(line, 100, f);
        EXPECT_STREQ("world 1\n", line);
        fclose(f);
    }