Commit 8d3ccb92 authored by Marc Vef's avatar Marc Vef
Browse files

Merge branch 'cmake_test_wr' into 'master'

test: add simple Write/Read test

See merge request zdvresearch_bsc/adafs!19
parents c96e825c 1602d42a
Loading
Loading
Loading
Loading
+16 −10
Original line number Diff line number Diff line
cmake_minimum_required(VERSION 3.7)
project(ifs_test)
cmake_minimum_required(VERSION 3.6)
project(ifs_test LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(MPI REQUIRED)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

include_directories(${MPI_INCLUDE_PATH})

set(SOURCE_FILES main.cpp)
set(SOURCE_FILES_MPI main_MPI.cpp)
set(SOURCE_FILES_IO main_IO_testing.cpp)
set(SOURCE_FILES_TEMP main_temp.cpp)
add_executable(ifs_test ${SOURCE_FILES})
add_executable(ifs_test_MPI ${SOURCE_FILES_MPI})

set(SOURCE_FILES_IO main_IO_testing.cpp)
add_executable(ifs_test_IO ${SOURCE_FILES_IO})

set(SOURCE_FILES_TEMP main_temp.cpp)
add_executable(ifs_test_temp ${SOURCE_FILES_TEMP})

target_link_libraries(ifs_test_MPI ${MPI_CXX_LIBRARIES})
 No newline at end of file
add_executable(ifs_test_wr wr_test.cpp)

find_package(MPI)
if(${MPI_FOUND})
    set(SOURCE_FILES_MPI main_MPI.cpp)
    add_executable(ifs_test_MPI ${SOURCE_FILES_MPI})
    target_link_libraries(ifs_test_MPI MPI::MPI_CXX)
endif()
 No newline at end of file

ifs_test/wr_test.cpp

0 → 100644
+64 −0
Original line number Diff line number Diff line
/* Simple Write/Read Test
 *
 * - open a file
 * - write some content
 * - close
 * - open the same file in read mode
 * - read the content
 * - check if the content match
 * - close
 */

#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <cstring>

using namespace std;

int main(int argc, char* argv[]) {

    string p = "/tmp/mountdir/file"s;
    char buffIn[] = "oops.";
    char *buffOut = new char[strlen(buffIn)];

    
    /* Write the file */

    auto fd = open(p.c_str(), O_WRONLY | O_CREAT, 0777);
    if(fd < 0){
        cerr << "Error opening file (write)" << endl;
        return -1;
    }
    auto nw = write(fd, buffIn, strlen(buffIn));
    if(nw != strlen(buffIn)){
        cerr << "Error writing file" << endl;
        return -1;
    }
    
    if(close(fd) != 0){
        cerr << "Error closing file" << endl;
        return -1;
    }


    /* Read the file back */

    fd = open(p.c_str(), O_RDONLY);
    if(fd < 0){
        cerr << "Error opening file (read)" << endl;
        return -1;
    }

    auto nr = read(fd, buffOut, strlen(buffIn));
    if(nr != strlen(buffIn)){
        cerr << "Error reading file" << endl;
        return -1;
    }

    if(strncmp( buffIn, buffOut, strlen(buffIn)) != 0){
        cerr << "File content mismatch" << endl;
        return -1;
    }
    close(fd);
}