int main(int argc, char* argv[]) {
    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << " INFILE\n";
        exit(1);
    }

    std::string output_format("SQLite");
    std::string input_filename(argv[1]);
    std::string output_filename("multipolygon.db");

    OGRDataSource* data_source = initialize_database(output_format, output_filename);

    osmium::area::ProblemReporterOGR problem_reporter(data_source);
    osmium::area::Assembler::config_type assembler_config(&problem_reporter);
    assembler_config.enable_debug_output();
    osmium::area::MultipolygonCollector<osmium::area::Assembler> collector(assembler_config);

    std::cerr << "Pass 1...\n";
    osmium::io::Reader reader1(input_filename);
    collector.read_relations(reader1);
    reader1.close();
    std::cerr << "Pass 1 done\n";

    index_type index_pos;
    index_type index_neg;
    location_handler_type location_handler(index_pos, index_neg);
    location_handler.ignore_errors();

    TestHandler test_handler(data_source);

    std::cerr << "Pass 2...\n";
    osmium::io::Reader reader2(input_filename);
    osmium::apply(reader2, location_handler, test_handler, collector.handler([&test_handler](const osmium::memory::Buffer& area_buffer) {
        osmium::apply(area_buffer, test_handler);
    }));
    reader2.close();
    std::cerr << "Pass 2 done\n";

    OGRDataSource::DestroyDataSource(data_source);
    OGRCleanupAll();
}
bool test_timer (ACE_Condition_Thread_Mutex& condition_, ACE_Time_Value& waittime, bool monotonic = false)
{
  bool status = true;
  MyTask task1;
  task1.create_reactor ();
  task1.start (1);
  TestHandler test_handler (task1.get_reactor ());

  // The second reactor that uses a hrtimer will trigger a timeout in
  // 5 seconds. At the same moment we calculate a timeout for the condition
  // 3 seconds in the future. Than we set the time 4 seconds back.
  // at the moment now the condition timeouts the trigger should have not
  // been executed. This is because with hrtime the trigger will call in 5
  // seconds and the condition in 3 seconds, independent of any time change
  // meaning that the timeout should trigger
  if (!test_handler.trigger_in (ACE_Time_Value (5, 0)))
    ACE_ERROR_RETURN ((LM_ERROR,
                        "(%P|%t) Unable to schedule trigger.\n"),
                      false);

  waittime += ACE_Time_Value (3,0);

  // reset system clock 4 seconds backwards
  ACE_Time_Value curtime = ACE_OS::gettimeofday ();
  curtime -= ACE_Time_Value (4, 0);
# if defined (ACE_WIN32)
  ACE_Date_Time curdt (curtime);
  SYSTEMTIME sys_time;
  sys_time.wDay = ACE_Utils::truncate_cast <WORD> (curdt.day ());
  sys_time.wMonth = ACE_Utils::truncate_cast <WORD> (curdt.month ());
  sys_time.wYear = ACE_Utils::truncate_cast <WORD> (curdt.year ());
  sys_time.wHour = ACE_Utils::truncate_cast <WORD> (curdt.hour ());
  sys_time.wMinute = ACE_Utils::truncate_cast <WORD> (curdt.minute ());
  sys_time.wSecond = ACE_Utils::truncate_cast <WORD> (curdt.second ());
  sys_time.wMilliseconds = ACE_Utils::truncate_cast <WORD> (curdt.microsec () / 1000);
  if (!::SetLocalTime (&sys_time))
# else
  timespec_t curts;
  curts = curtime;
  if (ACE_OS::clock_settime (CLOCK_REALTIME, &curts) != 0)
# endif
    {
      ACE_DEBUG((LM_INFO,
                  "(%P|%t) Unable to reset OS time. Insufficient privileges or not supported.\n"));
    }
  else
    {
      ACE_DEBUG((LM_INFO,
                  "(%P|%t) Going to wait on condition until %#T.\n", &waittime));
      if (condition_.wait (&waittime) != -1 || errno != ETIME)
        {
          ACE_ERROR ((LM_ERROR, "ERROR: No errno or return -1\n"));
          status = 1;
        }
      ACE_DEBUG((LM_INFO,
                  "(%P|%t) Condition wait returned at %#T.\n", &waittime));

      if (test_handler.timeout_triggered ())
        {
          if (monotonic)
            {
              ACE_ERROR ((LM_ERROR, "(%P|%t) ERROR: timer handler shouldn't have "
              "triggered because we used monotonic condition timing!\n"));
              status = false;
            }
          else
            ACE_DEBUG ((LM_INFO, "(%P|%t) timer handler "
            "triggered because we used non-monotonic condition timing!\n"));
        }
      else
        {
          if (!monotonic)
            {
              ACE_ERROR ((LM_ERROR, "(%P|%t) ERROR: timer handler should have "
              "triggered because we used non-monotonic condition timing!\n"));
              status = false;
            }
          else
            ACE_DEBUG ((LM_INFO, "(%P|%t) timer handler has not "
            "triggered because we used monotonic condition timing!\n"));
        }

      // reset system clock to correct time
      curtime = ACE_OS::gettimeofday ();
      curtime += ACE_Time_Value (4, 0);
# if defined (ACE_WIN32)
      curdt.update (curtime);
      SYSTEMTIME sys_time;
      sys_time.wDay = ACE_Utils::truncate_cast <WORD> (curdt.day ());
      sys_time.wMonth = ACE_Utils::truncate_cast <WORD> (curdt.month ());
      sys_time.wYear = ACE_Utils::truncate_cast <WORD> (curdt.year ());
      sys_time.wHour = ACE_Utils::truncate_cast <WORD> (curdt.hour ());
      sys_time.wMinute = ACE_Utils::truncate_cast <WORD> (curdt.minute ());
      sys_time.wSecond = ACE_Utils::truncate_cast <WORD> (curdt.second ());
      sys_time.wMilliseconds = ACE_Utils::truncate_cast <WORD> (curdt.microsec () / 1000);
      if (!::SetLocalTime (&sys_time))
# else
      curts = curtime;
      if (ACE_OS::clock_settime (CLOCK_REALTIME, &curts) != 0)
# endif
        {
          ACE_DEBUG((LM_INFO,
                      "(%P|%t) Unable to reset OS time. Insufficient privileges or not supported.\n"));
        }
    }

  ACE_DEBUG((LM_INFO,
              "(%P|%t) Asking worker thread to finish.\n"));
  task1.stop ();

  ACE_Thread_Manager::instance ()->wait ();

  return status;
}
static bool
timeout_test (void)
{
  bool status = true;
  SYNCH_QUEUE mq;
  MyTask task1;
  task1.create_reactor ();
  task1.start (1);
  TestHandler test_handler (task1.get_reactor (), mq);

  // The reactor of taks1 that uses a hrtimer will trigger a timeout in
  // 5 seconds which will enqueue a message block in the queue. At the
  // same moment we calculate a timeout for the dequeue operation for
  // 3 seconds in the future. Than we set the system time 4 seconds back.
  // The condition should timeout because the queue is empty and the trigger
  // only fires after the condition has timed out.
  // Next we start another dequeue for 3 seconds in the future which should
  // return before timing out because by then the trigger should have fired.
  // In case of using regular system time policy for message queue and
  // dequeue timeouts the first dequeue would not have timed out because
  // between calculating the timeout and starting the dequeue the system time
  // shifted back 4 sec causing the trigger to fire before the timeout elapsed.
  // In case timeshifting does not work because of priority problems or such
  // the test should succeed.

  if (!test_handler.trigger_in (ACE_Time_Value (5, 0)))
    ACE_ERROR_RETURN ((LM_ERROR,
                        "(%P|%t) Unable to schedule trigger.\n"),
                      false);

  if (!mq.is_empty ())
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("New queue is not empty!\n")));
      status = false;
    }
  else
    {
      ACE_Message_Block *b = 0;
      ACE_Time_Value_T<ACE_Monotonic_Time_Policy> tv;
      tv = (tv.now () + ACE_Time_Value (3,0)); // Now (monotonic time) + 3 sec

      // shift back in time 4 sec
      set_system_time (ACE_OS::gettimeofday () - ACE_Time_Value (4, 0));

      if (mq.dequeue_head (b, &tv) != -1)
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("Dequeued before timeout elapsed!\n")));
          status = false;
        }
      else if (errno != EWOULDBLOCK)
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("%p\n"),
                      ACE_TEXT ("Dequeue timeout should be EWOULDBLOCK, got")));
          status = false;
        }
      else
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("First dequeue timed out: OK\n")));

          tv = (tv.now () + ACE_Time_Value (3,0)); // Now (monotonic time) + 3 sec
          if (mq.dequeue_head (b, &tv) != -1)
            {
              ACE_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("Second dequeue succeeded: OK\n")));
              delete b;
            }
          else
            {
              ACE_ERROR ((LM_ERROR,
                          ACE_TEXT ("Second dequeue timed out!\n")));
              status = false;
            }
        }

      // restore time
      set_system_time (ACE_OS::gettimeofday () + ACE_Time_Value (4, 0));
    }

  ACE_DEBUG((LM_INFO,
              "(%P|%t) Asking worker thread to finish.\n"));
  task1.stop ();

  ACE_Thread_Manager::instance ()->wait ();

  return status;
}