Got rid of tests for UTF7, added error checking, adding test to check for null paramater for codepage

This commit is contained in:
Aaron 2015-08-06 10:16:13 -07:00
parent 8bf19f7144
commit 316cabbdcf
5 changed files with 62 additions and 32 deletions

View file

@ -8,10 +8,10 @@ include_directories(../ext-src/gtest/fused-src impl)
link_directories(${monad_native_BINARY_DIR})
# source file definitions
set(LIB_SOURCE_FILES impl/getcurrentprocessorid.cpp impl/getusername.cpp impl/terminal.cpp)
set(LIB_SOURCE_FILES impl/getcurrentprocessorid.cpp impl/getusername.cpp impl/terminal.cpp impl/getcpinfo.cpp)
set(HOST_COMMON_SOURCE_FILES host/common/coreclrutil.cpp)
set(HOST_COMMON_TEST_SOURCE_FILES tests/host/test-hostutil.cpp)
set(TEST_SOURCE_FILES tests/test-getcurrentprocessid.cpp ${HOST_COMMON_SOURCE_FILES} ${HOST_COMMON_TEST_SOURCE_FILES})
set(TEST_SOURCE_FILES tests/test-getcurrentprocessid.cpp tests/test-getcpinfo.cpp ${HOST_COMMON_SOURCE_FILES} ${HOST_COMMON_TEST_SOURCE_FILES})
set(SOURCE_FILES main.cpp ../ext-src/gtest/fused-src/gtest/gtest-all.cc)
SET(HOST_CMDLINE_SOURCE_FILES host/cmdline/main.cpp ${HOST_COMMON_SOURCE_FILES})

View file

@ -1,28 +1,50 @@
#include "getcpinfo.h"
#include <unistd.h>
#include <errno.h>
#include <string>
#include <langinfo.h>
BOOL GetCPInfo(UINT codepage, CPINFO &cpinfo)
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724432(v=vs.85).aspx
// Sets errno to:
// ERROR_INVALID_PARAMETER - parameter is not valid
// ERROR_BAD_ENVIRONMENT - locale is not UTF-8
//
// Returns:
// TRUE - succeeded
// FALSE - failed
BOOL GetCPInfoW(UINT codepage, CPINFO &cpinfo)
{
std::string test;
switch(codepage)
{
case 65000:
cpinfo.DefaultChar[0] = '?';
cpinfo.DefaultChar[1] = '0';
cpinfo.LeadByte[0] = '0';
cpinfo.LeadByte[1] = '0';
cpinfo.MaxCharSize = 5;
return TRUE;
case 65001:
const std::string utf8 = "UTF-8";
errno = 0;
//Check that codepage is not null
if(!codepage) {
errno = ERROR_INVALID_PARAMETER;
return FALSE;
}
// Select locale from environment
setlocale(LC_ALL, "");
// Check that locale is UTF-8
if (nl_langinfo(CODESET) != utf8) {
errno = ERROR_BAD_ENVIRONMENT;
return 0;
}
//if codepage is utf8
if(codepage == 65001) {
cpinfo.DefaultChar[0] = '?';
cpinfo.DefaultChar[1] = '0';
cpinfo.LeadByte[0] = '0';
cpinfo.LeadByte[1] = '0';
cpinfo.MaxCharSize = 4;
return TRUE;
default:
}
else{
errno = ERROR_INVALID_PARAMETER;
return FALSE;
}
}
}

View file

@ -4,6 +4,6 @@
PAL_BEGIN_EXTERNC
BOOL GetCPInfo(UINT codepage, CPINFO &cpinfo);
BOOL GetCPInfoW(UINT codepage, CPINFO &cpinfo);
PAL_END_EXTERNC

View file

@ -75,6 +75,8 @@
#define ERROR_INVALID_PARAMETER 87
#define ERROR_OUTOFMEMORY 14
#define MAX_PATH 0x00000104
#define UTF8 65001
#define ERROR_BAD_ENVIRONMENT 0x0000000A
typedef unsigned long long uint64;
#endif

View file

@ -1,14 +1,14 @@
#include <gtest/gtest.h>
#include "getcpinfo.h"
// This is a very simple test case to show how tests can be written
TEST(GetCPInfo,utf8)
// This test is with correct parameters
TEST(GetCPInfo,Utf8)
{
CPINFO cpinfo;
BOOL utf8 = GetCPInfo(65001, cpinfo);
BOOL result = GetCPInfoW(UTF8, cpinfo);
// first make sure that the function worked
ASSERT_TRUE(utf8 == TRUE);
ASSERT_TRUE(result == TRUE);
// now compare the actual values
ASSERT_EQ(cpinfo.DefaultChar[0],'?');
@ -18,18 +18,24 @@ TEST(GetCPInfo,utf8)
ASSERT_EQ(cpinfo.MaxCharSize,4);
}
TEST(GetCPInfo,utf7)
// This test is with codepage being null
TEST(GetCPInfo, NullForCodePageUINTButNotCpinfo)
{
CPINFO cpinfo;
BOOL utf7 = GetCPInfo(65000, cpinfo);
BOOL result = GetCPInfoW(NULL, cpinfo);
// first make sure that the function worked
ASSERT_TRUE(utf7 == TRUE);
ASSERT_TRUE(result == FALSE);
EXPECT_EQ(errno, ERROR_INVALID_PARAMETER);
}
// now compare the actual values
ASSERT_EQ(cpinfo.DefaultChar[0],'?');
ASSERT_EQ(cpinfo.DefaultChar[1],'0');
ASSERT_EQ(cpinfo.LeadByte[0],'0');
ASSERT_EQ(cpinfo.LeadByte[1],'0');
ASSERT_EQ(cpinfo.MaxCharSize,5);
// This test is with codepage not being utf8
TEST(GetCPInfo, CodePageNotUTF8)
{
CPINFO cpinfo;
BOOL result = GetCPInfoW(65000, cpinfo);
ASSERT_TRUE(result == FALSE);
EXPECT_EQ(errno, ERROR_INVALID_PARAMETER);
}