<commit_before>/**
 * Copyright (C) 2005-2007 <NAME> (<EMAIL>).
 * All rights reserved. See file LICENSE for licence and copyright
 * information.
 *
 * unit tests for endian.h
 *
 */

#include <cppunit/extensions/HelperMacros.h>
#include <ham/hamsterdb.h>
#include "../src/os.h"

#if WIN32
#   include <windows.h>
#else
#   include <unistd.h>
#endif

static void
my_errhandler(const char *message)
{
    (void)message;
}

class OsTest : public CppUnit::TestFixture
{
    CPPUNIT_TEST_SUITE(OsTest);
    CPPUNIT_TEST      (openCloseTest);
    CPPUNIT_TEST      (openReadOnlyCloseTest);
    CPPUNIT_TEST      (negativeOpenCloseTest);
    CPPUNIT_TEST      (createCloseTest);
    CPPUNIT_TEST      (createCloseOverwriteTest);
    CPPUNIT_TEST      (closeTest);
    CPPUNIT_TEST      (readWriteTest);
    CPPUNIT_TEST      (pagesizeTest);
    CPPUNIT_TEST      (mmapTest);
    CPPUNIT_TEST      (multipleMmapTest);
    CPPUNIT_TEST      (negativeMmapTest);
    CPPUNIT_TEST      (seekTellTest);
    CPPUNIT_TEST      (negativeSeekTest);
    CPPUNIT_TEST      (truncateTest);
    CPPUNIT_TEST      (largefileTest);
    CPPUNIT_TEST_SUITE_END();

public:
    void setUp()    
    { 
        ham_set_errhandler(my_errhandler);
    }

    void tearDown() 
    { 
#if WIN32
        (void)DeleteFileA((LPCSTR)".test");
#else
        (void)unlink(".test");
#endif
    }

    void openCloseTest()
    {
        ham_status_t st;
        ham_fd_t fd;

#if WIN32
        st=os_open("../../unittests/Makefile.am", 0, &fd);
#else
        st=os_open("Makefile", 0, &fd);
#endif
        CPPUNIT_ASSERT(st==0);
        st=os_close(fd, 0);
        CPPUNIT_ASSERT(st==0);
    }

    void openReadOnlyCloseTest()
    {
        ham_status_t st;
        ham_fd_t fd;
        const char *p="# XXXXXXXXX ERROR\n";

#if WIN32
        st=os_open("../../unittests/Makefile.am", HAM_READ_ONLY, &fd);
#else
        st=os_open("Makefile", HAM_READ_ONLY, &fd);
#endif
        CPPUNIT_ASSERT(st==0);
        st=os_pwrite(fd, 0, p, (ham_size_t)strlen(p));
        CPPUNIT_ASSERT(st==HAM_IO_ERROR);
        st=os_close(fd, 0);
        CPPUNIT_ASSERT(st==0);
    }

    void negativeOpenCloseTest()
    {
        ham_status_t st;
        ham_fd_t fd;

        st=os_open("__98324kasdlf.blöd", 0, &fd);
        CPPUNIT_ASSERT(st==HAM_FILE_NOT_FOUND);
    }

    void createCloseTest()
    {
        ham_status_t st;
        ham_fd_t fd;

        st=os_create(".test", 0, 0664, &fd);
        CPPUNIT_ASSERT_EQUAL(0, st);
        st=os_close(fd, 0);
        CPPUNIT_ASSERT_EQUAL(0, st);
    }

    void createCloseOverwriteTest()
    {
        ham_fd_t fd;
        ham_offset_t filesize;

        for (int i=0; i<3; i++) {
            CPPUNIT_ASSERT(os_create(".test", 0, 0664, &fd)==HAM_SUCCESS);
            CPPUNIT_ASSERT(os_seek(fd, 0, HAM_OS_SEEK_END)==HAM_SUCCESS);
            CPPUNIT_ASSERT(os_tell(fd, &filesize)==HAM_SUCCESS);
            CPPUNIT_ASSERT(filesize==0);
            CPPUNIT_ASSERT(os_truncate(fd, 1024)==HAM_SUCCESS);
            CPPUNIT_ASSERT(os_seek(fd, 0, HAM_OS_SEEK_END)==HAM_SUCCESS);
            CPPUNIT_ASSERT(os_tell(fd, &filesize)==HAM_SUCCESS);
            CPPUNIT_ASSERT(filesize==1024);
            CPPUNIT_ASSERT(os_close(fd, 0)==HAM_SUCCESS);
        }
    }

    void closeTest()
    {
#ifndef WIN32  // crashs in ntdll.dll
        ham_status_t st;

		st=os_close((ham_fd_t)0x12345, 0);
        CPPUNIT_ASSERT(st==HAM_IO_ERROR);
#endif
    }

    void readWriteTest()
    {
        int i;
        ham_status_t st;
        ham_fd_t fd;
        char buffer[128], orig[128];

        st=os_create(".test", 0, 0664, &fd);
        CPPUNIT_ASSERT(st==0);
        for (i=0; i<10; i++) {
            memset(buffer, i, sizeof(buffer));
            st=os_pwrite(fd, i*sizeof(buffer), buffer, sizeof(buffer));
            CPPUNIT_ASSERT(st==0);
        }<commit_msg>test for HAM_LOCK_EXCLUSIVE<commit_after>/**
 * Copyright (C) 2005-2007 <NAME> (<EMAIL>).
 * All rights reserved. See file LICENSE for licence and copyright
 * information.
 *
 * unit tests for endian.h
 *
 */

#include <cppunit/extensions/HelperMacros.h>
#include <ham/hamsterdb.h>
#include "../src/os.h"

#if WIN32
#   include <windows.h>
#else
#   include <unistd.h>
#endif

static void
my_errhandler(const char *message)
{
    (void)message;
}

class OsTest : public CppUnit::TestFixture
{
    CPPUNIT_TEST_SUITE(OsTest);
    CPPUNIT_TEST      (openCloseTest);
    CPPUNIT_TEST      (openReadOnlyCloseTest);
    CPPUNIT_TEST      (negativeOpenCloseTest);
    CPPUNIT_TEST      (createCloseTest);
    CPPUNIT_TEST      (createCloseOverwriteTest);
    CPPUNIT_TEST      (closeTest);
    CPPUNIT_TEST      (openExclusiveTest);
    CPPUNIT_TEST      (readWriteTest);
    CPPUNIT_TEST      (pagesizeTest);
    CPPUNIT_TEST      (mmapTest);
    CPPUNIT_TEST      (multipleMmapTest);
    CPPUNIT_TEST      (negativeMmapTest);
    CPPUNIT_TEST      (seekTellTest);
    CPPUNIT_TEST      (negativeSeekTest);
    CPPUNIT_TEST      (truncateTest);
    CPPUNIT_TEST      (largefileTest);
    CPPUNIT_TEST_SUITE_END();

public:
    void setUp()    
    { 
        ham_set_errhandler(my_errhandler);
    }

    void tearDown() 
    { 
#if WIN32
        (void)DeleteFileA((LPCSTR)".test");
#else
        (void)unlink(".test");
#endif
    }

    void openCloseTest()
    {
        ham_status_t st;
        ham_fd_t fd;

#if WIN32
        st=os_open("../../unittests/Makefile.am", 0, &fd);
#else
        st=os_open("Makefile", 0, &fd);
#endif
        CPPUNIT_ASSERT(st==0);
        st=os_close(fd, 0);
        CPPUNIT_ASSERT(st==0);
    }

    void openReadOnlyCloseTest()
    {
        ham_status_t st;
        ham_fd_t fd;
        const char *p="# XXXXXXXXX ERROR\n";

#if WIN32
        st=os_open("../../unittests/Makefile.am", HAM_READ_ONLY, &fd);
#else
        st=os_open("Makefile", HAM_READ_ONLY, &fd);
#endif
        CPPUNIT_ASSERT(st==0);
        st=os_pwrite(fd, 0, p, (ham_size_t)strlen(p));
        CPPUNIT_ASSERT(st==HAM_IO_ERROR);
        st=os_close(fd, 0);
        CPPUNIT_ASSERT(st==0);
    }

    void negativeOpenCloseTest()
    {
        ham_status_t st;
        ham_fd_t fd;

        st=os_open("__98324kasdlf.blöd", 0, &fd);
        CPPUNIT_ASSERT(st==HAM_FILE_NOT_FOUND);
    }

    void createCloseTest()
    {
        ham_status_t st;
        ham_fd_t fd;

        st=os_create(".test", 0, 0664, &fd);
        CPPUNIT_ASSERT_EQUAL(0, st);
        st=os_close(fd, 0);
        CPPUNIT_ASSERT_EQUAL(0, st);
    }

    void createCloseOverwriteTest()
    {
        ham_fd_t fd;
        ham_offset_t filesize;

        for (int i=0; i<3; i++) {
            CPPUNIT_ASSERT(os_create(".test", 0, 0664, &fd)==HAM_SUCCESS);
            CPPUNIT_ASSERT(os_seek(fd, 0, HAM_OS_SEEK_END)==HAM_SUCCESS);
            CPPUNIT_ASSERT(os_tell(fd, &filesize)==HAM_SUCCESS);
            CPPUNIT_ASSERT(filesize==0);
            CPPUNIT_ASSERT(os_truncate(fd, 1024)==HAM_SUCCESS);
            CPPUNIT_ASSERT(os_seek(fd, 0, HAM_OS_SEEK_END)==HAM_SUCCESS);
            CPPUNIT_ASSERT(os_tell(fd, &filesize)==HAM_SUCCESS);
            CPPUNIT_ASSERT(filesize==1024);
            CPPUNIT_ASSERT(os_close(fd, 0)==HAM_SUCCESS);
        }
    }

    void closeTest()
    {
#ifndef WIN32  // crashs in ntdll.dll
        ham_status_t st;

		st=os_close((ham_fd_t)0x12345, 0);
        CPPUNIT_ASSERT(st==HAM_IO_ERROR);
#endif
    }

    void openExclusiveTest()
    {
        ham_fd_t fd, fd2;

		CPPUNIT_ASSERT_EQUAL(0, os_create(".test", 
                    HAM_LOCK_EXCLUSIVE, 0664, &fd));
		CPPUNIT_ASSERT_EQUAL(0, os_close(fd, HAM_LOCK_EXCLUSIVE));
        
		CPPUNIT_ASSERT_EQUAL(0, os_open(".test", HAM_LOCK_EXCLUSIVE, &fd));
		CPPUNIT_ASSERT_EQUAL(HAM_WOULD_BLOCK, 
                os_open(".test", HAM_LOCK_EXCLUSIVE, &fd2));
		CPPUNIT_ASSERT_EQUAL(0, os_close(fd, HAM_LOCK_EXCLUSIVE));
		CPPUNIT_ASSERT_EQUAL(0, os_open(".test", HAM_LOCK_EXCLUSIVE, &fd2));
		CPPUNIT_ASSERT_EQUAL(0, os_close(fd2, HAM_LOCK_EXCLUSIVE));
		CPPUNIT_ASSERT_EQUAL(0, os_open(".test", 0, &fd2));
		CPPUNIT_ASSERT_EQUAL(0, os_close(fd2, 0));
    }

    void readWriteTest()
    {
        int i;
        ham_status_t st;
        ham_fd_t fd;
        char buffer[128], orig[128];

        st=os_create(".test", 0, 0664, &fd);
        CPPUNIT_ASSERT(st==0);
        for (i=0; i<10; i++) {
            memset(buffer, i, sizeof(buffer));
            st=os_pwrite(fd, i*sizeof(buffer), buffer, sizeof(buffer));
            CPPUNIT_ASSERT(st==0);
        }
{'fraction_non_alphanumeric': 0.13031161473087818, 'fraction_numerical': 0.021954674220963172, 'mean_word_length': 2.5994052676295665, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3576159', 'n_tokens_mistral': 3344, 'n_tokens_neox': 3086, 'n_words': 516}