Home | History | Annotate | Download | only in tests
      1 /* -*- c++ -*- */
      2 /*
      3  * Copyright (C) 2010 The Android Open Source Project
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  *  * Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  *  * Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in
     13  *    the documentation and/or other materials provided with the
     14  *    distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     20  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     23  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #include "../include/sstream"
     31 #ifndef ANDROID_ASTL_SSTREAM__
     32 #error "Wrong header included!!"
     33 #endif
     34 #include "common.h"
     35 
     36 #include <ios_base.h>
     37 #include <string>
     38 
     39 namespace android {
     40 using std::stringbuf;
     41 using std::stringstream;
     42 using std::string;
     43 
     44 bool testConstructor() {
     45     {
     46         string str("Get out of here and get me some money too.");
     47         stringbuf buf1(str);
     48         stringbuf buf2(str, std::ios_base::in);
     49         stringbuf buf3(str, std::ios_base::out);
     50 
     51         EXPECT_TRUE(buf1.str() == str);
     52         EXPECT_TRUE(buf2.str() == str);
     53         EXPECT_TRUE(buf3.str() == str);
     54     }
     55     return true;
     56 }
     57 
     58 bool testInAvail() {
     59     {
     60         string str("Get out of here and get me some money too.");
     61         stringbuf buf1(str);
     62         stringbuf buf2(str, std::ios_base::in);
     63         stringbuf buf3(str, std::ios_base::out);
     64         stringbuf buf4;
     65 
     66         std::streamsize len1 = buf1.in_avail();
     67         std::streamsize len2 = buf2.in_avail();
     68         std::streamsize len3 = buf3.in_avail();
     69         std::streamsize len4 = buf4.in_avail();
     70 
     71         EXPECT_TRUE(len1 > 0);
     72         EXPECT_TRUE(len2 > 0);
     73         EXPECT_TRUE(len3 == -1); // out only
     74         EXPECT_TRUE(len4 == 0); // out only
     75     }
     76     return true;
     77 }
     78 
     79 bool testNulChar() {
     80     string str("String with \0 in the middle", 27);
     81     stringbuf buf(str);
     82 
     83     EXPECT_TRUE(buf.in_avail() == 27);
     84     EXPECT_TRUE(buf.str().size() == 27);
     85     return true;
     86 }
     87 
     88 bool testPut() {
     89     stringbuf buf;
     90 
     91     buf.sputc('A');
     92     buf.sputc('B');
     93     buf.sputc('C');
     94     buf.sputc('D');
     95     EXPECT_TRUE(buf.str() == "ABCD");
     96 
     97     buf.sputn(" alphabet", 9);
     98     EXPECT_TRUE(buf.str() == "ABCD alphabet");
     99     return true;
    100 }
    101 
    102 bool testStringStream() {
    103     stringstream ss;
    104 
    105     ss << "This is: " << 10 << std::endl;
    106     EXPECT_TRUE(ss.str() == "This is: 10\n");
    107     return true;
    108 }
    109 }  // namespace android
    110 
    111 int main(int argc, char **argv){
    112     FAIL_UNLESS(testConstructor);
    113     FAIL_UNLESS(testInAvail);
    114     FAIL_UNLESS(testNulChar);
    115     FAIL_UNLESS(testPut);
    116     FAIL_UNLESS(testStringStream);
    117     return kPassed;
    118 }
    119