tests/runner.h
author David Anderson <danderson@mozilla.com>
Thu Aug 29 11:46:21 2013 -0700 (2013-08-29)
changeset 14 4ae6d413d3be
parent 11 cfe800908837
permissions -rw-r--r--
Sync AMTL.
danderson@11
     1
// vim: set sts=8 ts=2 sw=2 tw=99 et:
danderson@11
     2
//
danderson@11
     3
// Copyright (C) 2013, David Anderson and AlliedModders LLC
danderson@11
     4
// All rights reserved.
danderson@11
     5
// 
danderson@11
     6
// Redistribution and use in source and binary forms, with or without
danderson@11
     7
// modification, are permitted provided that the following conditions are met:
danderson@11
     8
// 
danderson@11
     9
//  * Redistributions of source code must retain the above copyright notice, this
danderson@11
    10
//    list of conditions and the following disclaimer.
danderson@11
    11
//  * Redistributions in binary form must reproduce the above copyright notice,
danderson@11
    12
//    this list of conditions and the following disclaimer in the documentation
danderson@11
    13
//    and/or other materials provided with the distribution.
danderson@11
    14
//  * Neither the name of AlliedModders LLC nor the names of its contributors
danderson@11
    15
//    may be used to endorse or promote products derived from this software
danderson@11
    16
//    without specific prior written permission.
danderson@11
    17
//
danderson@11
    18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
danderson@11
    19
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
danderson@11
    20
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
danderson@11
    21
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
danderson@11
    22
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
danderson@11
    23
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
danderson@11
    24
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
danderson@11
    25
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
danderson@11
    26
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
danderson@11
    27
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
danderson@11
    28
// POSSIBILITY OF SUCH DAMAGE.
danderson@11
    29
danderson@11
    30
#ifndef _include_amtl_runner_h_
danderson@11
    31
#define _include_amtl_runner_h_
danderson@11
    32
danderson@11
    33
#include <stdio.h>
danderson@11
    34
#include <stdarg.h>
danderson@11
    35
danderson@11
    36
namespace ke {
danderson@11
    37
danderson@11
    38
class Test
danderson@11
    39
{
danderson@11
    40
 public:
danderson@11
    41
  Test(const char *name)
danderson@11
    42
   : name_(name),
danderson@11
    43
     next_(head)
danderson@11
    44
  {
danderson@11
    45
    head = this;
danderson@11
    46
  }
danderson@11
    47
danderson@11
    48
  virtual bool Run() = 0;
danderson@11
    49
danderson@11
    50
  const char *name() const {
danderson@11
    51
    return name_;
danderson@11
    52
  }
danderson@11
    53
  Test *next() const {
danderson@11
    54
    return next_;
danderson@11
    55
  }
danderson@11
    56
danderson@11
    57
  static inline Test *first() {
danderson@11
    58
    return head;
danderson@11
    59
  }
danderson@11
    60
danderson@11
    61
 private:
danderson@11
    62
  static Test *head;
danderson@11
    63
danderson@11
    64
 private:
danderson@11
    65
  const char *name_;
danderson@11
    66
  Test *next_;
danderson@11
    67
};
danderson@11
    68
danderson@11
    69
static inline bool
danderson@11
    70
check(bool condition, const char *fmt, ...)
danderson@11
    71
{
danderson@11
    72
  FILE *fp = condition ? stdout : stderr;
danderson@11
    73
  if (condition)
danderson@11
    74
    fprintf(fp, " -- Ok: ");
danderson@11
    75
  else
danderson@11
    76
    fprintf(fp, " -- Failure: ");
danderson@11
    77
  va_list ap;
danderson@11
    78
  va_start(ap, fmt);
danderson@11
    79
  vfprintf(fp, fmt, ap);
danderson@11
    80
  va_end(ap);
danderson@11
    81
  fprintf(fp, "\n");
danderson@11
    82
  return condition;
danderson@11
    83
}
danderson@11
    84
danderson@11
    85
class FallibleMalloc
danderson@11
    86
{
danderson@11
    87
 public:
danderson@11
    88
  FallibleMalloc()
danderson@11
    89
   : shouldOutOfMemory_(false),
danderson@11
    90
     ooms_(0),
danderson@11
    91
     overflows_(0)
danderson@11
    92
  {
danderson@11
    93
  }
danderson@11
    94
danderson@11
    95
  void *malloc(size_t amount) {
danderson@11
    96
    if (shouldOutOfMemory_) {
danderson@11
    97
      reportOutOfMemory();
danderson@11
    98
      return NULL;
danderson@11
    99
    }
danderson@11
   100
    return ::malloc(amount);
danderson@11
   101
  }
danderson@11
   102
  void free(void *p) {
danderson@11
   103
    return ::free(p);
danderson@11
   104
  }
danderson@11
   105
  void reportOutOfMemory() {
danderson@11
   106
    ooms_++;
danderson@11
   107
  }
danderson@11
   108
  void reportAllocationOverflow() {
danderson@11
   109
    overflows_++;
danderson@11
   110
  }
danderson@11
   111
danderson@11
   112
  void setOutOfMemory(bool oom) {
danderson@11
   113
    shouldOutOfMemory_ = oom;
danderson@11
   114
  }
danderson@11
   115
  size_t ooms() const {
danderson@11
   116
    return ooms_;
danderson@11
   117
  }
danderson@11
   118
  size_t overflows() const {
danderson@11
   119
    return overflows_;
danderson@11
   120
  }
danderson@11
   121
danderson@11
   122
 private:
danderson@11
   123
  bool shouldOutOfMemory_;
danderson@11
   124
  size_t ooms_;
danderson@11
   125
  size_t overflows_;
danderson@11
   126
};
danderson@11
   127
danderson@11
   128
}
danderson@11
   129
danderson@11
   130
#endif // _include_amtl_runner_h_
danderson@11
   131