// file: verify.h
// tier: L1
// verify: clang++ -std=c++20 -I. src/<kebab>.cpp -o bin/<kebab> && ./bin/<kebab>
//
// OGRE 书稿 C++ lab 共享的轻量断言头（独立最小验证，非 OGRE 源码）。
// 零第三方依赖，仅 <cstdio>/<sstream>/<cstdlib>。
//
// 用法：
//   #include "verify.h"
//   CHECK(cond);            // 断言布尔条件
//   CHECK_EQ(a, b);         // 断言 a == b（失败打印双方值）
//   return lab::report("01-1 历史与定位");   // 汇总并返回退出码（失败非零）
//
// 每个 lab 是一个可自执行的 .cpp：自带 main()，编译即跑。
#ifndef OGRE_LAB_VERIFY_H
#define OGRE_LAB_VERIFY_H

#include <cstdio>
#include <cstdlib>
#include <sstream>

namespace lab {

struct Counter {
  int total = 0;
  int failed = 0;
  void fail(const char* expr, const char* file, int line) {
    ++failed;
    std::fprintf(stderr, "FAIL %s:%d: %s\n", file, line, expr);
  }
};

inline Counter& counter() {
  static Counter c;
  return c;
}

template <typename A, typename B>
void checkEq(const A& a, const B& b, const char* expr, const char* file, int line) {
  counter().total++;
  if (!(a == b)) {
    counter().fail(expr, file, line);
    std::ostringstream os;
    os << "  left=" << a << "  right=" << b;
    std::fprintf(stderr, "%s\n", os.str().c_str());
  }
}

inline int report(const char* name) {
  std::printf("%s: %d checks, %d failed\n", name, counter().total, counter().failed);
  return counter().failed == 0 ? 0 : 1;
}

}  // namespace lab

#define CHECK(cond)                                                       \
  do {                                                                    \
    ::lab::counter().total++;                                             \
    if (!(cond)) ::lab::counter().fail(#cond, __FILE__, __LINE__);        \
  } while (0)

#define CHECK_EQ(a, b) ::lab::checkEq((a), (b), #a " == " #b, __FILE__, __LINE__)

#endif  // OGRE_LAB_VERIFY_H
