LCOV - code coverage report
Current view: top level - estd/examples - string_view.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 43 43 100.0 %
Date: 2025-01-20 13:53:09 Functions: 6 9 66.7 %

          Line data    Source code
       1             : // Copyright 2024 Accenture.
       2             : 
       3             : #include "estd/string_view.h"
       4             : 
       5             : #include <gtest/gtest.h>
       6             : 
       7           3 : TEST(StringViewExample, assign)
       8             : {
       9             :     // [EXAMPLE_STRING_VIEW_ASSIGN_START]
      10           1 :     estd::string_view view;
      11           1 :     estd::string_view view2("Hello");
      12             : 
      13           1 :     view = view2;
      14           1 :     ASSERT_EQ('H', view[0]);
      15           1 :     ASSERT_EQ('e', view[1]);
      16           2 :     ASSERT_EQ(5U, view.size());
      17             :     // [EXAMPLE_STRING_VIEW_ASSIGN_END]
      18             : }
      19             : 
      20           3 : TEST(StringViewExample, swap)
      21             : {
      22             :     // [EXAMPLE_STRING_VIEW_SWAP_START]
      23           1 :     estd::string_view view  = estd::string_view("Hello");
      24           1 :     estd::string_view view2 = estd::string_view("Jellow");
      25             : 
      26           1 :     ASSERT_EQ('H', view[0]);
      27           1 :     ASSERT_EQ('e', view[1]);
      28           1 :     ASSERT_EQ(5U, view.size());
      29             : 
      30           1 :     ASSERT_EQ('J', view2[0]);
      31           1 :     ASSERT_EQ('e', view2[1]);
      32           1 :     ASSERT_EQ(6U, view2.size());
      33             : 
      34           1 :     view.swap(view2);
      35             : 
      36           1 :     ASSERT_EQ('H', view2[0]);
      37           1 :     ASSERT_EQ('e', view2[1]);
      38           1 :     ASSERT_EQ(5U, view2.size());
      39             : 
      40           1 :     ASSERT_EQ('J', view[0]);
      41           1 :     ASSERT_EQ('e', view[1]);
      42           2 :     ASSERT_EQ(6U, view.size());
      43             :     // [EXAMPLE_STRING_VIEW_SWAP_END]
      44             : }
      45             : 
      46           3 : TEST(StringViewExample, tokenize)
      47             : {
      48             :     // [EXAMPLE_STRING_VIEW_TOKENIZE_START]
      49           1 :     estd::string_view view   = estd::string_view("Hello World how is it going?");
      50           1 :     estd::string_view delims = estd::string_view(" ");
      51           1 :     estd::string_view expected_tokens[]
      52             :         = {estd::string_view("Hello"),
      53             :            estd::string_view("World"),
      54             :            estd::string_view("how"),
      55             :            estd::string_view("is"),
      56             :            estd::string_view("it"),
      57           1 :            estd::string_view("going?")};
      58             : 
      59           1 :     auto tokens = view.tokenize(delims);
      60           1 :     auto it     = tokens.begin();
      61             : 
      62           1 :     ASSERT_EQ(*it, expected_tokens[0]);
      63           1 :     ++it;
      64           1 :     ASSERT_EQ(*it, expected_tokens[1]);
      65           1 :     ++it;
      66           1 :     ASSERT_EQ(*it, expected_tokens[2]);
      67           1 :     ++it;
      68           1 :     ASSERT_EQ(*it, expected_tokens[3]);
      69           1 :     ++it;
      70           1 :     ASSERT_EQ(*it, expected_tokens[4]);
      71           1 :     ++it;
      72           1 :     ASSERT_EQ(*it, expected_tokens[5]);
      73           1 :     ++it;
      74           2 :     ASSERT_TRUE(it == tokens.end());
      75             :     // [EXAMPLE_STRING_VIEW_TOKENIZE_END]
      76             : }

Generated by: LCOV version 1.14