storage/test/test_binding_params.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/storage/test/test_binding_params.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,215 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "storage_test_harness.h"
    1.11 +
    1.12 +#include "mozStorageHelper.h"
    1.13 +
    1.14 +using namespace mozilla;
    1.15 +
    1.16 +/**
    1.17 + * This file tests binding and reading out string parameters through the
    1.18 + * mozIStorageStatement API.
    1.19 + */
    1.20 +
    1.21 +void
    1.22 +test_ASCIIString()
    1.23 +{
    1.24 +  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
    1.25 +
    1.26 +  // Create table with a single string column.
    1.27 +  (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
    1.28 +    "CREATE TABLE test (str STRING)"
    1.29 +  ));
    1.30 +
    1.31 +  // Create statements to INSERT and SELECT the string.
    1.32 +  nsCOMPtr<mozIStorageStatement> insert, select;
    1.33 +  (void)db->CreateStatement(NS_LITERAL_CSTRING(
    1.34 +    "INSERT INTO test (str) VALUES (?1)"
    1.35 +  ), getter_AddRefs(insert));
    1.36 +  (void)db->CreateStatement(NS_LITERAL_CSTRING(
    1.37 +    "SELECT str FROM test"
    1.38 +  ), getter_AddRefs(select));
    1.39 +
    1.40 +  // Roundtrip a string through the table, and ensure it comes out as expected.
    1.41 +  nsAutoCString inserted("I'm an ASCII string");
    1.42 +  {
    1.43 +    mozStorageStatementScoper scoper(insert);
    1.44 +    bool hasResult;
    1.45 +    do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, inserted)));
    1.46 +    do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult)));
    1.47 +    do_check_false(hasResult);
    1.48 +  }
    1.49 +
    1.50 +  nsAutoCString result;
    1.51 +  {
    1.52 +    mozStorageStatementScoper scoper(select);
    1.53 +    bool hasResult;
    1.54 +    do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
    1.55 +    do_check_true(hasResult);
    1.56 +    do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result)));
    1.57 +  }
    1.58 +
    1.59 +  do_check_true(result == inserted);
    1.60 +
    1.61 +  (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test"));
    1.62 +}
    1.63 +
    1.64 +void
    1.65 +test_CString()
    1.66 +{
    1.67 +  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
    1.68 +
    1.69 +  // Create table with a single string column.
    1.70 +  (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
    1.71 +    "CREATE TABLE test (str STRING)"
    1.72 +  ));
    1.73 +
    1.74 +  // Create statements to INSERT and SELECT the string.
    1.75 +  nsCOMPtr<mozIStorageStatement> insert, select;
    1.76 +  (void)db->CreateStatement(NS_LITERAL_CSTRING(
    1.77 +    "INSERT INTO test (str) VALUES (?1)"
    1.78 +  ), getter_AddRefs(insert));
    1.79 +  (void)db->CreateStatement(NS_LITERAL_CSTRING(
    1.80 +    "SELECT str FROM test"
    1.81 +  ), getter_AddRefs(select));
    1.82 +
    1.83 +  // Roundtrip a string through the table, and ensure it comes out as expected.
    1.84 +  static const char sCharArray[] =
    1.85 +    "I'm not a \xff\x00\xac\xde\xbb ASCII string!";
    1.86 +  nsAutoCString inserted(sCharArray, ArrayLength(sCharArray) - 1);
    1.87 +  do_check_true(inserted.Length() == ArrayLength(sCharArray) - 1);
    1.88 +  {
    1.89 +    mozStorageStatementScoper scoper(insert);
    1.90 +    bool hasResult;
    1.91 +    do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, inserted)));
    1.92 +    do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult)));
    1.93 +    do_check_false(hasResult);
    1.94 +  }
    1.95 +
    1.96 +  {
    1.97 +    nsAutoCString result;
    1.98 +
    1.99 +    mozStorageStatementScoper scoper(select);
   1.100 +    bool hasResult;
   1.101 +    do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
   1.102 +    do_check_true(hasResult);
   1.103 +    do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result)));
   1.104 +
   1.105 +    do_check_true(result == inserted);
   1.106 +  }
   1.107 +
   1.108 +  (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test"));
   1.109 +}
   1.110 +
   1.111 +void
   1.112 +test_UTFStrings()
   1.113 +{
   1.114 +  nsCOMPtr<mozIStorageConnection> db(getMemoryDatabase());
   1.115 +
   1.116 +  // Create table with a single string column.
   1.117 +  (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
   1.118 +    "CREATE TABLE test (str STRING)"
   1.119 +  ));
   1.120 +
   1.121 +  // Create statements to INSERT and SELECT the string.
   1.122 +  nsCOMPtr<mozIStorageStatement> insert, select;
   1.123 +  (void)db->CreateStatement(NS_LITERAL_CSTRING(
   1.124 +    "INSERT INTO test (str) VALUES (?1)"
   1.125 +  ), getter_AddRefs(insert));
   1.126 +  (void)db->CreateStatement(NS_LITERAL_CSTRING(
   1.127 +    "SELECT str FROM test"
   1.128 +  ), getter_AddRefs(select));
   1.129 +
   1.130 +  // Roundtrip a UTF8 string through the table, using UTF8 input and output.
   1.131 +  static const char sCharArray[] =
   1.132 +    "I'm a \xc3\xbb\xc3\xbc\xc3\xa2\xc3\xa4\xc3\xa7 UTF8 string!";
   1.133 +  nsAutoCString insertedUTF8(sCharArray, ArrayLength(sCharArray) - 1);
   1.134 +  do_check_true(insertedUTF8.Length() == ArrayLength(sCharArray) - 1);
   1.135 +  NS_ConvertUTF8toUTF16 insertedUTF16(insertedUTF8);
   1.136 +  do_check_true(insertedUTF8 == NS_ConvertUTF16toUTF8(insertedUTF16));
   1.137 +  {
   1.138 +    mozStorageStatementScoper scoper(insert);
   1.139 +    bool hasResult;
   1.140 +    do_check_true(NS_SUCCEEDED(insert->BindUTF8StringByIndex(0, insertedUTF8)));
   1.141 +    do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult)));
   1.142 +    do_check_false(hasResult);
   1.143 +  }
   1.144 +
   1.145 +  {
   1.146 +    nsAutoCString result;
   1.147 +
   1.148 +    mozStorageStatementScoper scoper(select);
   1.149 +    bool hasResult;
   1.150 +    do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
   1.151 +    do_check_true(hasResult);
   1.152 +    do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result)));
   1.153 +
   1.154 +    do_check_true(result == insertedUTF8);
   1.155 +  }
   1.156 +
   1.157 +  // Use UTF8 input and UTF16 output.
   1.158 +  {
   1.159 +    nsAutoString result;
   1.160 +
   1.161 +    mozStorageStatementScoper scoper(select);
   1.162 +    bool hasResult;
   1.163 +    do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
   1.164 +    do_check_true(hasResult);
   1.165 +    do_check_true(NS_SUCCEEDED(select->GetString(0, result)));
   1.166 +
   1.167 +    do_check_true(result == insertedUTF16);
   1.168 +  }
   1.169 +
   1.170 +  (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test"));
   1.171 +
   1.172 +  // Roundtrip the same string using UTF16 input and UTF8 output.
   1.173 +  {
   1.174 +    mozStorageStatementScoper scoper(insert);
   1.175 +    bool hasResult;
   1.176 +    do_check_true(NS_SUCCEEDED(insert->BindStringByIndex(0, insertedUTF16)));
   1.177 +    do_check_true(NS_SUCCEEDED(insert->ExecuteStep(&hasResult)));
   1.178 +    do_check_false(hasResult);
   1.179 +  }
   1.180 +
   1.181 +  {
   1.182 +    nsAutoCString result;
   1.183 +
   1.184 +    mozStorageStatementScoper scoper(select);
   1.185 +    bool hasResult;
   1.186 +    do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
   1.187 +    do_check_true(hasResult);
   1.188 +    do_check_true(NS_SUCCEEDED(select->GetUTF8String(0, result)));
   1.189 +
   1.190 +    do_check_true(result == insertedUTF8);
   1.191 +  }
   1.192 +
   1.193 +  // Use UTF16 input and UTF16 output.
   1.194 +  {
   1.195 +    nsAutoString result;
   1.196 +
   1.197 +    mozStorageStatementScoper scoper(select);
   1.198 +    bool hasResult;
   1.199 +    do_check_true(NS_SUCCEEDED(select->ExecuteStep(&hasResult)));
   1.200 +    do_check_true(hasResult);
   1.201 +    do_check_true(NS_SUCCEEDED(select->GetString(0, result)));
   1.202 +
   1.203 +    do_check_true(result == insertedUTF16);
   1.204 +  }
   1.205 +
   1.206 +  (void)db->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM test"));
   1.207 +}
   1.208 +
   1.209 +void (*gTests[])(void) = {
   1.210 +  test_ASCIIString,
   1.211 +  test_CString,
   1.212 +  test_UTFStrings,
   1.213 +};
   1.214 +
   1.215 +const char *file = __FILE__;
   1.216 +#define TEST_NAME "binding string params"
   1.217 +#define TEST_FILE file
   1.218 +#include "storage_test_harness_tail.h"

mercurial