/ externals / xbyak / test / noexception.cpp
noexception.cpp
  1  #define XBYAK_NO_EXCEPTION
  2  #include <xbyak/xbyak.h>
  3  
  4  using namespace Xbyak;
  5  
  6  int g_err = 0;
  7  int g_test = 0;
  8  
  9  void assertEq(int x, int y)
 10  {
 11  	if (x != y) {
 12  		printf("ERR x=%d y=%d\n", x, y);
 13  		g_err++;
 14  	}
 15  	g_test++;
 16  }
 17  
 18  void assertBool(bool b)
 19  {
 20  	if (!b) {
 21  		printf("ERR assertBool\n");
 22  		g_err++;
 23  	}
 24  	g_test++;
 25  }
 26  
 27  void test1()
 28  {
 29  	const int v = 123;
 30  	struct Code : CodeGenerator {
 31  		Code()
 32  		{
 33  			mov(eax, v);
 34  			ret();
 35  		}
 36  	} c;
 37  	int (*f)() = c.getCode<int (*)()>();
 38  	assertEq(f(), v);
 39  	assertEq(Xbyak::GetError(), ERR_NONE);
 40  }
 41  
 42  void test2()
 43  {
 44  	struct Code : CodeGenerator {
 45  		Code()
 46  		{
 47  			Label lp;
 48  			L(lp);
 49  			L(lp);
 50  		}
 51  	} c;
 52  	assertEq(Xbyak::GetError(), ERR_LABEL_IS_REDEFINED);
 53  	Xbyak::ClearError();
 54  }
 55  
 56  void test3()
 57  {
 58  	static struct EmptyAllocator : Xbyak::Allocator {
 59  		uint8_t *alloc(size_t) { return 0; }
 60  	} emptyAllocator;
 61  	struct Code : CodeGenerator {
 62  		Code() : CodeGenerator(8, 0, &emptyAllocator)
 63  		{
 64  			mov(eax, 3);
 65  			assertBool(Xbyak::GetError() == 0);
 66  			mov(eax, 3);
 67  			mov(eax, 3);
 68  			assertBool(Xbyak::GetError() != 0);
 69  			Xbyak::ClearError();
 70  			assertBool(Xbyak::GetError() == 0);
 71  		}
 72  	} c;
 73  }
 74  
 75  void test4()
 76  {
 77  	struct Code : CodeGenerator {
 78  		Code()
 79  		{
 80  			mov(ptr[eax], 1);
 81  			assertBool(Xbyak::GetError() != 0);
 82  			Xbyak::ClearError();
 83  
 84  			test(ptr[eax], 1);
 85  			assertBool(Xbyak::GetError() != 0);
 86  			Xbyak::ClearError();
 87  
 88  			adc(ptr[eax], 1);
 89  			assertBool(Xbyak::GetError() != 0);
 90  			Xbyak::ClearError();
 91  
 92  			setz(eax);
 93  			assertBool(Xbyak::GetError() != 0);
 94  			Xbyak::ClearError();
 95  		}
 96  	};
 97  }
 98  
 99  int main()
100  {
101  	test1();
102  	test2();
103  	test3();
104  	test4();
105  	if (g_err) {
106  		printf("err %d/%d\n", g_err, g_test);
107  	} else {
108  		printf("all ok %d\n", g_test);
109  	}
110  	return g_err != 0;
111  }