t-exc.c revision 302408
1254721Semaste/* 2254721Semaste * Copyright (c) 2000-2001 Proofpoint, Inc. and its suppliers. 3353358Sdim * All rights reserved. 4353358Sdim * 5353358Sdim * By using this file, you agree to the terms and conditions set 6254721Semaste * forth in the LICENSE file which can be found at the top level of 7254721Semaste * the sendmail distribution. 8254721Semaste */ 9254721Semaste 10254721Semaste#include <sm/gen.h> 11254721SemasteSM_IDSTR(id, "@(#)$Id: t-exc.c,v 1.21 2013-11-22 20:51:43 ca Exp $") 12254721Semaste 13254721Semaste#include <string.h> 14254721Semaste#include <sm/heap.h> 15254721Semaste#include <sm/io.h> 16254721Semaste#include <sm/test.h> 17254721Semaste 18254721Semasteconst SM_EXC_TYPE_T EtypeTest1 = 19341825Sdim{ 20254721Semaste SmExcTypeMagic, 21254721Semaste "E:test1", 22254721Semaste "i", 23254721Semaste sm_etype_printf, 24254721Semaste "test1 exception argv[0]=%0", 25254721Semaste}; 26314564Sdim 27314564Sdimconst SM_EXC_TYPE_T EtypeTest2 = 28314564Sdim{ 29314564Sdim SmExcTypeMagic, 30314564Sdim "E:test2", 31314564Sdim "i", 32314564Sdim sm_etype_printf, 33254721Semaste "test2 exception argv[0]=%0", 34314564Sdim}; 35254721Semaste 36341825Sdimint 37314564Sdimmain(argc, argv) 38254721Semaste int argc; 39314564Sdim char **argv; 40314564Sdim{ 41314564Sdim void *p; 42314564Sdim int volatile x; 43314564Sdim char *unknown, *cant; 44314564Sdim 45314564Sdim sm_test_begin(argc, argv, "test exception handling"); 46314564Sdim 47314564Sdim /* 48314564Sdim ** SM_TRY 49314564Sdim */ 50254721Semaste 51314564Sdim cant = "can't happen"; 52254721Semaste x = 0; 53353358Sdim SM_TRY 54254721Semaste x = 1; 55314564Sdim SM_END_TRY 56314564Sdim SM_TEST(x == 1); 57314564Sdim 58314564Sdim /* 59314564Sdim ** SM_FINALLY-0 60254721Semaste */ 61314564Sdim 62314564Sdim x = 0; 63314564Sdim SM_TRY 64254721Semaste x = 1; 65341825Sdim SM_FINALLY 66314564Sdim x = 2; 67314564Sdim SM_END_TRY 68254721Semaste SM_TEST(x == 2); 69314564Sdim 70314564Sdim /* 71314564Sdim ** SM_FINALLY-1 72314564Sdim */ 73314564Sdim 74314564Sdim x = 0; 75314564Sdim SM_TRY 76314564Sdim SM_TRY 77314564Sdim x = 1; 78254721Semaste sm_exc_raisenew_x(&EtypeTest1, 17); 79254721Semaste SM_FINALLY 80 x = 2; 81 sm_exc_raisenew_x(&EtypeTest2, 42); 82 SM_END_TRY 83 SM_EXCEPT(exc, "E:test2") 84 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 85 "got exception test2: can't happen\n"); 86 SM_EXCEPT(exc, "E:test1") 87 SM_TEST(x == 2 && exc->exc_argv[0].v_int == 17); 88 if (!(x == 2 && exc->exc_argv[0].v_int == 17)) 89 { 90 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 91 "can't happen: x=%d argv[0]=%d\n", 92 x, exc->exc_argv[0].v_int); 93 } 94 SM_EXCEPT(exc, "*") 95 { 96 unknown = "unknown exception: "; 97 SM_TEST(strcmp(unknown, cant) == 0); 98 } 99 SM_END_TRY 100 101 x = 3; 102 SM_TRY 103 x = 4; 104 sm_exc_raisenew_x(&EtypeTest1, 94); 105 SM_FINALLY 106 x = 5; 107 sm_exc_raisenew_x(&EtypeTest2, 95); 108 SM_EXCEPT(exc, "E:test2") 109 { 110 unknown = "got exception test2: "; 111 SM_TEST(strcmp(unknown, cant) == 0); 112 } 113 SM_EXCEPT(exc, "E:test1") 114 SM_TEST(x == 5 && exc->exc_argv[0].v_int == 94); 115 if (!(x == 5 && exc->exc_argv[0].v_int == 94)) 116 { 117 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 118 "can't happen: x=%d argv[0]=%d\n", 119 x, exc->exc_argv[0].v_int); 120 } 121 SM_EXCEPT(exc, "*") 122 { 123 unknown = "unknown exception: "; 124 SM_TEST(strcmp(unknown, cant) == 0); 125 } 126 SM_END_TRY 127 128 SM_TRY 129 sm_exc_raisenew_x(&SmEtypeErr, "test %d", 0); 130 SM_EXCEPT(exc, "*") 131#if DEBUG 132 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 133 "test 0 got an exception, as expected:\n"); 134 sm_exc_print(exc, smioout); 135#endif /* DEBUG */ 136 return sm_test_end(); 137 SM_END_TRY 138 139 p = sm_malloc_x((size_t)(-1)); 140 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 141 "sm_malloc_x unexpectedly succeeded, returning %p\n", p); 142 unknown = "sm_malloc_x unexpectedly succeeded"; 143 SM_TEST(strcmp(unknown, cant) == 0); 144 return sm_test_end(); 145} 146