/** * expmodtest.c * A simple test of the expmod function in SamR's simple math library. * * Copyright (c) 2022 Samuel A. Rebelsky. All rights reserved. * * This file is part of SRMath, SamR's simple math library. * * SRMath is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * SRMath is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with SRMath. If not, see . */ // +-------+----------------------------------------------------------- // | Notes | // +-------+ // +---------+--------------------------------------------------------- // | Headers | // +---------+ #include "srmath.h" #include "srtest.h" #include #include // +--------+---------------------------------------------------------- // | Macros | // +--------+ // +---------+------------------------------------------------------- // | Helpers | // +---------+ long dumbmod (long x, long n, long m) { long result = 1; for (long i = 0; i < n; i++) { result = (result * x) % m; } return result; } // dumbmod void random_test () { long x = random () % INT_MAX; long n = random () % 100; long m = random (); SR_CHECK_LONG (stderr, expmod (x, n, m), dumbmod (x, n, m)); } // +------+------------------------------------------------------------ // | Main | // +------+ int main (int argc, char *argv[]) { sr_reset(); // Easy ones that we can write by hand SR_CHECK_LONG (stderr, expmod (2, 3, 4), 0); SR_CHECK_LONG (stderr, expmod (5, 6, 7), 1); SR_CHECK_LONG (stderr, expmod (3, 3, 2), 1); SR_CHECK_LONG (stderr, expmod (3, 2, 1), 0); SR_CHECK_LONG (stderr, expmod (5, 3, 10), 5); // Harder ones SR_CHECK_LONG (stderr, expmod (17, 93, 1033), dumbmod (17, 93, 1033)); // Random ones random_test (); random_test (); random_test (); random_test (); random_test (); if (sr_get_errors() != 0) { sr_report (stderr); return 1; } // if (sr_errors != 0) // And we're done return 0; } // main