modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/ken/divmod.go (about)

     1  // run
     2  
     3  // Copyright 2009 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Test integer division and modulus.
     8  
     9  package main
    10  
    11  const (
    12  	// example from the spec
    13  	n1 = +5
    14  	n2 = -5
    15  	d1 = +3
    16  	d2 = -3
    17  
    18  	q1 = +1
    19  	q2 = -1
    20  	q3 = -1
    21  	q4 = +1
    22  
    23  	r1 = +2
    24  	r2 = -2
    25  	r3 = +2
    26  	r4 = -2
    27  )
    28  
    29  func main() {
    30  	/* ideals */
    31  	if n1/d1 != q1 || n1%d1 != r1 {
    32  		println("ideal-1", n1, d1, n1/d1, n1%d1)
    33  		panic("fail")
    34  	}
    35  	if n2/d1 != q2 || n2%d1 != r2 {
    36  		println("ideal-2", n2, d1, n2/d1, n2%d1)
    37  		panic("fail")
    38  	}
    39  	if n1/d2 != q3 || n1%d2 != r3 {
    40  		println("ideal-3", n1, d2, n1/d2, n1%d2)
    41  		panic("fail")
    42  	}
    43  	if n2/d2 != q4 || n2%d2 != r4 {
    44  		println("ideal-4", n2, d2, n2/d2, n2%d2)
    45  		panic("fail")
    46  	}
    47  
    48  	/* int */
    49  	var in1 int = +5
    50  	var in2 int = -5
    51  	var id1 int = +3
    52  	var id2 int = -3
    53  
    54  	if in1/id1 != q1 || in1%id1 != r1 {
    55  		println("int-1", in1, id1, in1/id1, in1%id1)
    56  		panic("fail")
    57  	}
    58  	if in2/id1 != q2 || in2%id1 != r2 {
    59  		println("int-2", in2, id1, in2/id1, in2%id1)
    60  		panic("fail")
    61  	}
    62  	if in1/id2 != q3 || in1%id2 != r3 {
    63  		println("int-3", in1, id2, in1/id2, in1%id2)
    64  		panic("fail")
    65  	}
    66  	if in2/id2 != q4 || in2%id2 != r4 {
    67  		println("int-4", in2, id2, in2/id2, in2%id2)
    68  		panic("fail")
    69  	}
    70  
    71  	/* int8 */
    72  	var bn1 int8 = +5
    73  	var bn2 int8 = -5
    74  	var bd1 int8 = +3
    75  	var bd2 int8 = -3
    76  
    77  	if bn1/bd1 != q1 || bn1%bd1 != r1 {
    78  		println("int8-1", bn1, bd1, bn1/bd1, bn1%bd1)
    79  		panic("fail")
    80  	}
    81  	if bn2/bd1 != q2 || bn2%bd1 != r2 {
    82  		println("int8-2", bn2, bd1, bn2/bd1, bn2%bd1)
    83  		panic("fail")
    84  	}
    85  	if bn1/bd2 != q3 || bn1%bd2 != r3 {
    86  		println("int8-3", bn1, bd2, bn1/bd2, bn1%bd2)
    87  		panic("fail")
    88  	}
    89  	if bn2/bd2 != q4 || bn2%bd2 != r4 {
    90  		println("int8-4", bn2, bd2, bn2/bd2, bn2%bd2)
    91  		panic("fail")
    92  	}
    93  
    94  	/* int16 */
    95  	var sn1 int16 = +5
    96  	var sn2 int16 = -5
    97  	var sd1 int16 = +3
    98  	var sd2 int16 = -3
    99  
   100  	if sn1/sd1 != q1 || sn1%sd1 != r1 {
   101  		println("int16-1", sn1, sd1, sn1/sd1, sn1%sd1)
   102  		panic("fail")
   103  	}
   104  	if sn2/sd1 != q2 || sn2%sd1 != r2 {
   105  		println("int16-2", sn2, sd1, sn2/sd1, sn2%sd1)
   106  		panic("fail")
   107  	}
   108  	if sn1/sd2 != q3 || sn1%sd2 != r3 {
   109  		println("int16-3", sn1, sd2, sn1/sd2, sn1%sd2)
   110  		panic("fail")
   111  	}
   112  	if sn2/sd2 != q4 || sn2%sd2 != r4 {
   113  		println("int16-4", sn2, sd2, sn2/sd2, sn2%sd2)
   114  		panic("fail")
   115  	}
   116  
   117  	/* int32 */
   118  	var ln1 int32 = +5
   119  	var ln2 int32 = -5
   120  	var ld1 int32 = +3
   121  	var ld2 int32 = -3
   122  
   123  	if ln1/ld1 != q1 || ln1%ld1 != r1 {
   124  		println("int32-1", ln1, ld1, ln1/ld1, ln1%ld1)
   125  		panic("fail")
   126  	}
   127  	if ln2/ld1 != q2 || ln2%ld1 != r2 {
   128  		println("int32-2", ln2, ld1, ln2/ld1, ln2%ld1)
   129  		panic("fail")
   130  	}
   131  	if ln1/ld2 != q3 || ln1%ld2 != r3 {
   132  		println("int32-3", ln1, ld2, ln1/ld2, ln1%ld2)
   133  		panic("fail")
   134  	}
   135  	if ln2/ld2 != q4 || ln2%ld2 != r4 {
   136  		println("int32-4", ln2, ld2, ln2/ld2, ln2%ld2)
   137  		panic("fail")
   138  	}
   139  
   140  	/* int64 */
   141  	var qn1 int64 = +5
   142  	var qn2 int64 = -5
   143  	var qd1 int64 = +3
   144  	var qd2 int64 = -3
   145  
   146  	if qn1/qd1 != q1 || qn1%qd1 != r1 {
   147  		println("int64-1", qn1, qd1, qn1/qd1, qn1%qd1)
   148  		panic("fail")
   149  	}
   150  	if qn2/qd1 != q2 || qn2%qd1 != r2 {
   151  		println("int64-2", qn2, qd1, qn2/qd1, qn2%qd1)
   152  		panic("fail")
   153  	}
   154  	if qn1/qd2 != q3 || qn1%qd2 != r3 {
   155  		println("int64-3", qn1, qd2, qn1/qd2, qn1%qd2)
   156  		panic("fail")
   157  	}
   158  	if qn2/qd2 != q4 || qn2%qd2 != r4 {
   159  		println("int64-4", qn2, qd2, qn2/qd2, qn2%qd2)
   160  		panic("fail")
   161  	}
   162  
   163  	if n1/qd1 != q1 || n1%qd1 != r1 {
   164  		println("mixed int64-1", n1, qd1, n1/qd1, n1%qd1)
   165  		panic("fail")
   166  	}
   167  	if n2/qd1 != q2 || n2%qd1 != r2 {
   168  		println("mixed int64-2", n2, qd1, n2/qd1, n2%qd1)
   169  		panic("fail")
   170  	}
   171  	if n1/qd2 != q3 || n1%qd2 != r3 {
   172  		println("mixed int64-3", n1, qd2, n1/qd2, n1%qd2)
   173  		panic("fail")
   174  	}
   175  	if n2/qd2 != q4 || n2%qd2 != r4 {
   176  		println("mixed int64-4", n2, qd2, n2/qd2, n2%qd2)
   177  		panic("fail")
   178  	}
   179  
   180  	if qn1/d1 != q1 || qn1%d1 != r1 {
   181  		println("mixed int64-5", qn1, d1, qn1/d1, qn1%d1)
   182  		panic("fail")
   183  	}
   184  	if qn2/d1 != q2 || qn2%d1 != r2 {
   185  		println("mixed int64-6", qn2, d1, qn2/d1, qn2%d1)
   186  		panic("fail")
   187  	}
   188  	if qn1/d2 != q3 || qn1%d2 != r3 {
   189  		println("mixed int64-7", qn1, d2, qn1/d2, qn1%d2)
   190  		panic("fail")
   191  	}
   192  	if qn2/d2 != q4 || qn2%d2 != r4 {
   193  		println("mixed int64-8", qn2, d2, qn2/d2, qn2%d2)
   194  		panic("fail")
   195  	}
   196  
   197  	/* uint */
   198  	var uin1 uint = +5
   199  	var uid1 uint = +3
   200  
   201  	if uin1/uid1 != q1 || uin1%uid1 != r1 {
   202  		println("uint", uin1, uid1, uin1/uid1, uin1%uid1)
   203  		panic("fail")
   204  	}
   205  
   206  	/* uint8 */
   207  	var ubn1 uint8 = +5
   208  	var ubd1 uint8 = +3
   209  
   210  	if ubn1/ubd1 != q1 || ubn1%ubd1 != r1 {
   211  		println("uint8", ubn1, ubd1, ubn1/ubd1, ubn1%ubd1)
   212  		panic("fail")
   213  	}
   214  
   215  	/* uint16 */
   216  	var usn1 uint16 = +5
   217  	var usd1 uint16 = +3
   218  
   219  	if usn1/usd1 != q1 || usn1%usd1 != r1 {
   220  		println("uint16", usn1, usd1, usn1/usd1, usn1%usd1)
   221  		panic("fail")
   222  	}
   223  
   224  	/* uint32 */
   225  	var uln1 uint32 = +5
   226  	var uld1 uint32 = +3
   227  
   228  	if uln1/uld1 != q1 || uln1%uld1 != r1 {
   229  		println("uint32", uln1, uld1, uln1/uld1, uln1%uld1)
   230  		panic("fail")
   231  	}
   232  
   233  	/* uint64 */
   234  	var uqn1 uint64 = +5
   235  	var uqd1 uint64 = +3
   236  
   237  	if uqn1/uqd1 != q1 || uqn1%uqd1 != r1 {
   238  		println("uint64", uqn1, uqd1, uqn1/uqd1, uqn1%uqd1)
   239  		panic("fail")
   240  	}
   241  	if n1/uqd1 != q1 || n1%uqd1 != r1 {
   242  		println("mixed uint64-1", n1, uqd1, n1/uqd1, n1%uqd1)
   243  		panic("fail")
   244  	}
   245  	if uqn1/d1 != q1 || uqn1%d1 != r1 {
   246  		println("mixed uint64-2", uqn1, d1, uqn1/d1, uqn1%d1)
   247  		panic("fail")
   248  	}
   249  }