TEST(SliceContainerTest, FromSlice) {
  std::shared_ptr<Builder> builder = BuildValue("\"this is a string of 20 bytes\"");

  Slice slice = builder->slice();
  SliceContainer sb(slice);
  
  ASSERT_EQ(sb.byteSize(), slice.byteSize());
  ASSERT_EQ(0, memcmp(sb.data(), slice.begin(), slice.byteSize()));
  ASSERT_EQ(29UL, sb.byteSize());
  ASSERT_TRUE(sb.slice().isString());
  ASSERT_EQ("this is a string of 20 bytes", sb.slice().copyString());
  ASSERT_NE(sb.data(), slice.begin());
} 
Exemple #2
0
static void signal_safe_write_data(Slice data) {
#if TD_PORT_POSIX
  while (!data.empty()) {
    auto res = write(2, data.begin(), data.size());
    if (res < 0 && errno == EINTR) {
      continue;
    }
    if (res <= 0) {
      break;
    }

    if (res > 0) {
      data.remove_prefix(res);
    }
  }
#elif TD_PORT_WINDOWS
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
  HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
  DWORD bytes_written;
  WriteFile(stderr_handle, data.data(), static_cast<DWORD>(data.size()), &bytes_written, nullptr);
#else
// there is no stderr
#endif
#endif
}
Exemple #3
0
void Base64::encode(const Slice& bytes, std::string* output) {
  output->assign(ToBase64Iter(bytes.begin()), ToBase64Iter(bytes.end()));

  // Boost does not handle Base64 padding.
  const auto num_padding_chars = (3 - (bytes.size() % 3)) % 3;
  output->append(num_padding_chars, '=');
}
TEST(SliceContainerTest, MoveAssign) {
  std::shared_ptr<Builder> builder = BuildValue("\"this is a string of 20 bytes\"");

  Slice slice = builder->slice();
  SliceContainer sb(slice.begin(), slice.byteSize());
  SliceContainer sb2(slice.begin(), slice.byteSize());
  sb2 = std::move(sb);
 
  ASSERT_TRUE(sb.slice().isNone()); // must be empty now 
  ASSERT_EQ(1UL, sb.slice().byteSize());

  ASSERT_EQ(29UL, sb2.byteSize());
  ASSERT_TRUE(sb2.slice().isString());
  ASSERT_EQ("this is a string of 20 bytes", sb2.slice().copyString());

  ASSERT_NE(sb.slice().begin(), sb2.slice().begin());
} 
Exemple #5
0
void perturb(Slice& slice, Number_type epsilon) {
  list<string> components;
  slice.components(back_inserter(components));
  for (list<string>::const_iterator it = components.begin();
       it != components.end();
       ++it)
  {
    for (Slice::Contour_iterator cit = slice.begin(*it);
	 cit != slice.end(*it);
	 ++cit)
    {
      perturb((*cit)->polygon(), epsilon);
    }
  }
}
TEST(SliceContainerTest, FromSliceLonger) {
  std::string s("[-1");
  for (size_t i = 0; i < 2000; ++i) {   
    s.push_back(',');
    s.append(std::to_string(i));
  } 
  s.push_back(']');

  std::shared_ptr<Builder> builder = BuildValue(s);

  Slice slice = builder->slice();
  SliceContainer sb(slice);
  
  ASSERT_EQ(sb.byteSize(), slice.byteSize());
  ASSERT_EQ(0, memcmp(sb.data(), builder->start(), slice.byteSize()));
  ASSERT_TRUE(sb.slice().isArray());
  ASSERT_NE(sb.data(), slice.begin());
} 
TEST(SliceContainerTest, SizeLengthByteSize) {
  std::shared_ptr<Builder> builder = BuildValue("\"this is a string of 20 bytes\"");

  Slice slice = builder->slice();
  SliceContainer sb(slice.begin(), slice.byteSize());
 
  ASSERT_TRUE(sb.slice().isString()); 
  ASSERT_EQ(29UL, sb.size());
  ASSERT_EQ(29UL, sb.length());
  ASSERT_EQ(29UL, sb.byteSize());
  ASSERT_EQ(sb.data(), sb.begin());

  Slice empty;
  sb = SliceContainer(empty);
  
  ASSERT_EQ(1UL, sb.size());
  ASSERT_EQ(1UL, sb.length());
  ASSERT_EQ(1UL, sb.byteSize());
  ASSERT_EQ(sb.data(), sb.begin());
} 
Exemple #8
0
void print_ser(std::ostream& out, const Slice& slice, Number_type thickness)
{
    list<string> components;
    slice.components(back_inserter(components));

    out << "<?xml version=\"1.0\"?>" << endl;
    out << "<!DOCTYPE Section SYSTEM \"section.dtd\">" << endl;
    out << endl;
    out << "<Section index=\"87\" thickness=\"" << thickness << "\" alignLocked=\"true\">" << endl;

    for (list<string>::const_iterator it = components.begin(); it != components.end(); ++it) {
        string component(*it);

        out << "<Transform dim=\"0\"" << endl;
        out << "xcoef=\" 0 1 0 0 0 0\"" << endl;
        out << "ycoef=\" 0 0 1 0 0 0\">" << endl;

        typedef Slice::Contour_const_iterator C_iter;
        for (C_iter c_it = slice.begin(component); c_it != slice.end(component); ++c_it) {
            Contour_handle contour = *c_it;
            const Polygon_2& polygon = contour->polygon();

            out << "<Contour name=\"" << component << "\" hidden=\"true\" closed=\"true\" simplified=\"true\" border=\"0 1 0\" fill=\"0 1 0\" mode=\"9\"" << endl;
            out << "points=\"";

            // insert points here
            Polygon_2::Vertex_const_iterator  vit;
            for (vit = polygon.vertices_begin(); vit != polygon.vertices_end(); ++vit) {
                out << "\t" << vit->x() << " " << vit->y() << "," << std::endl;
            }

            out << "\t\"/>" << endl;
        }
        out << "</Transform>" << endl;
        out << endl;
    }

    out << "</Section>" << endl;
    out << endl;
    out << endl;
}
Exemple #9
0
std::string pp(const Slice& slice)
{
    std::stringstream out;
    out << std::setprecision(pp_precision);

    list<string> components;
    slice.components(back_inserter(components));
    for (list<string>::const_iterator it = components.begin();
            it != components.end();
            ++it)
    {
        out << *it << ": ";
        for (Slice::Contour_const_iterator cit = slice.begin(*it);
                cit != slice.end(*it);
                ++cit)
        {
            out << pp((*cit)->polygon());
        }
    }

    return out.str();
}